开发者

How to capture the standard output/error of a Process?

开发者 https://www.devze.com 2023-01-14 21:38 出处:网络
How does one capture the s开发者_StackOverflowtandard output/error of a process started by a Process.Start() to a string?To solve the deadlock problems use this approach:

How does one capture the s开发者_StackOverflowtandard output/error of a process started by a Process.Start() to a string?


To solve the deadlock problems use this approach:

ProcessStartInfo hanging on "WaitForExit"? Why?

Works well in my code...


By redirecting it and reading the stream.


Sample code is below:

        ProcessStartInfo psi = new ProcessStartInfo();
        psi.CreateNoWindow = false;
        psi.UseShellExecute = false;
        psi.FileName = "C:\\my.exe";
        psi.WindowStyle = ProcessWindowStyle.Hidden;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;

        using (Process exeProcess = Process.Start(psi))
        {
            exeProcess.WaitForExit();

            var exitCode = exeProcess.ExitCode;
            var output = exeProcess.StandardOutput.ReadToEnd();
            var error = exeProcess.StandardError.ReadToEnd();

            if (output.Length > 0)
            {
                // you have some output
            }


            if(error.Length > 0)
            {
                // you have some error details
            }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消