重定向进程输出 C# [英] Redirect process output C#

查看:27
本文介绍了重定向进程输出 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将进程的标准输出重定向到一个字符串以供以后解析.我还想在进程运行时在屏幕上看到输出,而不仅仅是在运行完成时.

I would like to redirect the Process's standard output to a string for later parsing. I would also like to see the output on the screen, while the process is running, and not only when it finishes it's run.

这可能吗?

推荐答案

使用 重定向标准输出.

来自 MSDN 的示例:

Sample from MSDN:

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

另见 OutputDataReceivedBeginOutputReadLine() 用于替代ReadToEnd(),这将更好地满足您在进程运行时查看输出"的要求.

Also see OutputDataReceived and BeginOutputReadLine() for an alternative to ReadToEnd(), that will better fulfill your "see output while the process is running" requirement.

这篇关于重定向进程输出 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆