如何捕获由Process.Start()
启动到string
的流程的标准输出/错误?
答案 0 :(得分:1)
答案 1 :(得分:-1)
按redirecting it并阅读信息流。
答案 2 :(得分:-1)
示例代码如下:
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
}
}