捕获标准输出,仍然在控制台窗口中显示它 [英] Capture standard output and still display it in the console window

查看:174
本文介绍了捕获标准输出,仍然在控制台窗口中显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成一个运行在可视的控制台窗口(这是一个运行的MSBuild批处理文件)子进程,我想有在可见的控制台窗口中显示的过程中产生的输出,以及捕获输出,所以我可以在code处理。我读过其他一些问题和MSDN文档处理ProcessStartInfo.RedirectStandardOutput之类的,我可以捕捉从重定向数据流输出和code处理它就好了:

I'm spawning a child process that runs in a visible console window (it's a batch file that runs MSBuild), and I'd like to have the output generated by the process displayed in the visible console window, as well as capture that output so I can process it in code. I've read several other questions and the MSDN documentation dealing with ProcessStartInfo.RedirectStandardOutput and the like, and I can capture the output from the redirected stream and process it in code just fine:

Process msBuild = new Process();
msBuild.StartInfo.FileName = "Build.bat";
msBuild.StartInfo.UseShellExecute = false;
msBuild.StartInfo.RedirectStandardOutput = true;
msBuild.Start();
string output = msBuild.StandardOutput.ReadToEnd();
msBuild.WaitForExit();

的问题是,输出未在子进程的控制台窗口中显示;我只是得到,而运行进程的屏幕,当它结束后,即消失在一个空白的控制台窗口。

The problem is that the output is not displayed in the console window of the child process; I just get a blank console window on the screen while the process is running, which disappears when it's finished.

我想我可以隐藏实际的子进程的窗口,并显示我只会写的输出,因为它被抓获第二个窗口,但似乎更多的工作是必要的。有没有办法让在控制台窗口中显示的输出,仍然捕捉到它的处理当它这样做?

I suppose I could hide the actual child process window, and display a second window that I would simply write the output to as it was captured, but that seems like more work than is necessary. Is there a way to have the output displayed in the console window and still capture it for processing when it's done?

推荐答案

一旦你重定向标准输出它在控制台不再执导。要写入控制台,你将不得不做手工。

Once you've redirected standard out it's no longer directed at the console. To write to the console you'll have to do it manually.

如果你想为进程执行的,而不是在1日结束倾卸大,显示输出,则可以使用Process类的OutputDataReceived事件。

If you want to display output as the process executes, instead of in 1 big dump at the end, you can use the "OutputDataReceived" event of the Process class.

这篇关于捕获标准输出,仍然在控制台窗口中显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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