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

查看:172
本文介绍了重定向输出过程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.

是,即使是可能的?

推荐答案

使用的 RedirectStandardOutput

示例:

// 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();



另见的 OutputDataReceived 和的 BeginOutputReadLine() 获取到 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天全站免登陆