问题
I am running a command line utility using Process.Start. For debugging purposes I would like to have its output stream to Visual Studio's Debug Output panel (Debug.Write). I'd like to do this in real time rather than waiting for the process to complete and then writting it all at once.
I know this is possible in theory but I'm not experienced enough with the Stream object to know how to do this.
回答1:
This may not be exactly what you want, but it puts you on the right track, I think.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += p_OutputDataReceived;
p.Start();
p.BeginOutputReadLine();
Then, your event handler for receiving data.
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Debug.Write(e.Data);
}
来源:https://stackoverflow.com/questions/17370705/pipe-a-stream-to-debug-write