2

MSDN states that it is possible in .NET to capture the output of a process and display it in the console window at the same time.

Normally when you set StartInfo.RedirectStandardOutput = true; the console window stays blank. As the MSDN site doesn't provide a sample for this I was wondering if anyone would have a sample or could point me to a sample?

When a Process writes text to its standard stream, that text is normally displayed on the console. By redirecting the StandardOutput stream, you can manipulate or suppress the output of a process. For example, you can filter the text, format it differently, or write the output to both the console and a designated log file. MSDN

This post is similar to Capture standard output and still display it in the console window by the way. But that post didn't end up with a working sample.

Thanks a lot,

Patrick

Community
  • 1
  • 1
Patrick Wolf
  • 2,530
  • 2
  • 28
  • 27

2 Answers2

1

you can easily catch all messages using

Process build = new Process();
...
build.StartInfo.UseShellExecute = false;
build.StartInfo.RedirectStandardOutput = true;
build.StartInfo.RedirectStandardError = true;
build.StartInfo.CreateNoWindow = true;
build.ErrorDataReceived += build_ErrorDataReceived;
build.OutputDataReceived += build_ErrorDataReceived;
build.EnableRaisingEvents = true;
...

and create the Event build_ErrorDataReceived

static void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    string msg = e.Data;
    if (msg != null && msg.Length > 0)
    {
        // in msg you have the line you need!
    }
}

I add a little example

Screencast of the application

Solution Files (VS 2008)

Community
  • 1
  • 1
balexandre
  • 73,608
  • 45
  • 233
  • 342
  • > Normally when you set StartInfo.RedirectStandardOutput = true; the console window stays blank. This is what I don't want :) I like to get the result as a string + the console window should look as usual and display the messages as the occur. Thanks Patrick – Patrick Wolf Mar 18 '10 at 15:21
0

See this answer here on SO which I posted code for a process to exec netstat an redirect the output stream to a Stringbuilder instance. The process creates a hidden window and is not visible...

You can modify the code slightly by changing the values respectively

ps.CreateNoWindow = true; <--- Comment this out...
ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; <--- Comment this out...
Community
  • 1
  • 1
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • The problem is when I make the window visible it will be blank since the output is redirected. Thanks, Patrick – Patrick Wolf Mar 18 '10 at 15:23
  • @Patrick: Then...change the above ps.CreateNoWindow to true,and make the ps.WindowStyle to hidden...? – t0mm13b Mar 18 '10 at 17:34
  • Hi TommieB, sorry don't understand. The reason for my question was that I like to see the output in the command window as usual and I want the output in a string variable at the end. When you use StartInfo.RedirectStandardOutput = true; the console window stays blank... e.g. it doesn't look "as usual" :) – Patrick Wolf Mar 23 '10 at 02:34
  • @Patrick: Sorry, if you redirect output, you will not see anything! – t0mm13b Mar 25 '10 at 16:59