3

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.

George Mauer
  • 117,483
  • 131
  • 382
  • 612

1 Answers1

5

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);
}
Gray
  • 7,050
  • 2
  • 29
  • 52
  • 1
    Thank you sir - that's it exactly. Just one note, you can't use this technique AND `p.StandardOuptut.ReadToEnd()` You have to create a `StringBuilder` and build it up yourself in the event handler. Easy enough, just something to be aware of. – George Mauer Jun 28 '13 at 18:18
  • How can we pipe a custom data, such as a % of data processed to p_OutputDataReceived from calling scope – BGT Jan 28 '22 at 10:56