RedirectStandardOutput

时间:2023-03-09 08:16:52
RedirectStandardOutput

当Process将文本写入其标准流,通常在控制台上显示文本。

通过设置RedirectStandardOutput到true重定向StandardOutput流,可以操作或取消进程的输出。

例如,可以筛选文本、 格式设置不同,或将输出写入到控制台和一个指定的日志文件。

必须设置UseShellExecute到false如果你想要设置RedirectStandardOutput到true。

否则,从读取StandardOutput流将引发异常。

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();

string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();