Capture and display console output at the same time

前端 未结 2 1943
再見小時候
再見小時候 2021-01-22 11:09

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

相关标签:
2条回答
  • 2021-01-22 11:40

    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...
    
    0 讨论(0)
  • 2021-01-22 11:47

    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)

    0 讨论(0)
提交回复
热议问题