同时捕捉和显示控制台输出 [英] Capture and display console output at the same time
问题描述
MSDN说,它有可能在.NET中捕捉到一个进程的输出和在同一时间在控制台窗口中显示出来。
MSDN states that it is possible in .NET to capture the output of a process and display it in the console window at the same time.
通常情况下,当你设置StartInfo.RedirectStandardOutput = TRUE;控制台窗口保持空白。 由于MSDN网站不为此提供了一个示例中,我想知道是否有人将有一个样品或可能指向我一个样品?
Normally when you set StartInfo.RedirectStandardOutput = true; the console window stays blank. As the MSDN site doesn't provide a sample for this I was wondering if anyone would have a sample or could point me to a sample?
当一个进程写入文本的 标准的流,该文本通常是 在控制台上显示。通过 重定向StandardOutput 流,你可以操纵或燮preSS 一个过程的输出。例如, 您可以过滤文本,格式化 不同,或写输出 控制台和一个指定的日志 文件。 MSDN
When a Process writes text to its standard stream, that text is normally displayed on the console. By redirecting the StandardOutput stream, you can manipulate or suppress the output of a process. For example, you can filter the text, format it differently, or write the output to both the console and a designated log file. MSDN
这帖子是类似于<一href="http://stackoverflow.com/questions/786726/capture-standard-output-and-still-display-it-in-the-console-window">http://stackoverflow.com/questions/786726/capture-standard-output-and-still-display-it-in-the-console-window顺便一提。但是,后并没有就此结束了工作示例。
This post is similar to http://stackoverflow.com/questions/786726/capture-standard-output-and-still-display-it-in-the-console-window by the way. But that post didn't end up with a working sample.
感谢了很多,
帕特里克
推荐答案
您可以轻松地使用捕捉所有邮件
you can easily catch all messages using
Process build = new Process();
...
build.StartInfo.UseShellExecute = false;
build.StartInfo.RedirectStandardOutput = true;
build.StartInfo.RedirectStandardError = true;
build.StartInfo.CreateNoWindow = true;
build.ErrorDataReceived += build_ErrorDataReceived;
build.OutputDataReceived += build_ErrorDataReceived;
build.EnableRaisingEvents = true;
...
和创建活动build_ErrorDataReceived
and create the Event build_ErrorDataReceived
static void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
string msg = e.Data;
if (msg != null && msg.Length > 0)
{
// in msg you have the line you need!
}
}
我添加了一个小例子
应用的截屏
解决方案文件(VS 2008)
这篇关于同时捕捉和显示控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!