Reputation: 171
All I want is to redirect standard output of a process to a file. Sounds easy, but everything I tried doesn't work:
putting a DOS-style redirection in the list of arguments (e.g. param1 param2 > output.txt
) doesn't work;
using RedirectStandardOutput = true
works, BUT, apparently the process does not raise an event when it exists. So the handler defined via process.Exited += ...
doesn't execute. To be clear, once I remove the RedirectStandardOutput = true
statement, it DOES raise an event.
What am I doing wrong ?
Upvotes: 3
Views: 1148
Reputation: 7735
Method #2 must be the way to go.
The problem seems to be caused by output buffering which prevents triggering Exited
event.
You should consider eliminating the Exited
event handler. Instead, subscribe to OutputDataReceived
event and check Process.HasExited
property in the handler to perform cleanup job:
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
...
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Process p = (Process) sender;
if(p.HasExited) ...
}
Upvotes: 1