捕获从控制台应用程序输出的字符! [英] Capture characters output from console app!

查看:61
本文介绍了捕获从控制台应用程序输出的字符!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获控制台应用程序输出的字符!
我尝试了所有可用的方法,但是直到打印该行之前都无法捕获它!
(在打印行之前,读取行根本无法工作)

Hi, i want to capture characters output from console app!
i have tried all method thats available but cannot capture it until the line is printed!
(read line is not working at all until the line is printed)
Any idea?

推荐答案

不,您当然没有尝试过所有方法". :-)

捕获方法是众所周知的,因为所有IDE都需要它.是好是坏,但传统上,所有编译器和其他构建工具(如MSBuild.EXE)都是作为单独的命令行应用程序创建的,并由IDE在单独的进程中运行.例如,Visual Studio从不构建.NET项目,而是使用编译器,MSBuild和其他与(免费重新发行).NET Framework版本捆绑在一起的其他工具.但是,您永远都不会看到原始"控制台,因为如果使用IDE,所有输出(实际上也是流:标准输出和标准错误)都将重定向到UI.
您可以通过将方法System.Diagnostics.Process.Start(ProcessStartInfo)与重定向的StandardOutputStandardError一起使用来实现.您应该记住设置标志System.Diagnostics.ProcessStartInfo.RedirectStandardOutputSystem.Diagnostics.ProcessStartInfo.RedirectStandardError.

参见:
http://msdn.microsoft.com/en-us/library/system.diagnostics. process.aspx [^ ],
http://msdn.microsoft.com/en-us/library/system.diagnostics. processstartinfo.aspx [ ^ ].

您可以在此处找到用于重定向和基本说明的简单代码示例: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx [
No, you certainly did not try "all methods". :-)

The method of capturing is well known, because all IDE need it. Good or bad, but traditionally all compilers and other build tools (like MSBuild.EXE) are created as separate command line applications and run in separate processes by IDE. For example, Visual Studio never builds .NET projects but uses compilers, MSBuild and other tools bundled with every version of (freely redistributable) .NET Framework. Nevertheless, you never see "raw" console, because all the output (in fact, too streams: standard output and standard error) are redirected to UI if the IDE.

You can do it by using the method System.Diagnostics.Process.Start(ProcessStartInfo) with re-directed StandardOutput and StandardError. You should remember to set the flags System.Diagnostics.ProcessStartInfo.RedirectStandardOutput and System.Diagnostics.ProcessStartInfo.RedirectStandardError.

See:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx[^].

You can find a simple code sample for redirection and basic explanations here: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx[^].

—SA


这篇关于捕获从控制台应用程序输出的字符!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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