代码之家  ›  专栏  ›  技术社区  ›  Louis Rhys

如何捕获流程的标准输出/错误?

  •  3
  • Louis Rhys  · 技术社区  · 15 年前

    如何捕获由 Process.Start() string ?

    3 回复  |  直到 13 年前
        1
  •  -1
  •   Darin Dimitrov    15 年前

    redirecting it 阅读溪流。

        2
  •  1
  •   Community Mohan Dere    9 年前

    要解决死锁问题,请使用以下方法:

    ProcessStartInfo hanging on "WaitForExit"? Why?

        3
  •  -1
  •   bev    12 年前

            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
                }
            }