Capture and display console output at the same time

前端 未结 2 1945
再見小時候
再見小時候 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: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)

提交回复
热议问题