如何使用缓冲区(管道)读取控制台输出 [英] How to read console output with buffer (pipe)

查看:110
本文介绍了如何使用缓冲区(管道)读取控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VB.NET 2010,我想使用缓冲区实时读取控制台输出.
因为有些控制台应用程序在完成该操作之前不会在输出中显示行.

请提供任何帮助!

Hi, I''m using VB.NET 2010 and i want to read console output in real time with buffer.
Because there is some console application that doesn''t show line in output until th operation is done.

Any help please!

推荐答案

您可以将控制台应用程序的输出重定向到您可以自己提供的某些流中.使用System.Diagnostics.Process.Start(ProcessStartInfo).

您可以重定向两个输出流:System.Diagnostics.Process.StandardErrorSystem.Diagnostics.Process.StandardOutput.您需要使用System.Diagnostics.ProcessStartInfo来指定System.Diagnostics.ProcessStartInfo.RedirectStandardErrorSystem.Diagnostics.ProcessStartInfo.RedirectStandardErrorRedirectStandardOutput.

您可以将文件流用于StandardErrorStandardOutput或其他流的重定向,因为这些属性的类型应为System.IO.StreamReader.您可以创建自己的派生自System.IO.StreamReader的类,也可以使用其构造函数接受抽象类System.IO.Stream的参数,因此也可以使用自己的派生自System.IO.Stream的流类.这样,您可以根据需要实现要用于流重定向的任何流类型.

(此重定向功能的一个典型示例是在IDE下运行编译器或链接器(或用于构建步骤的其他工具),其中在IDE控件中显示编译器或链接器的输出(尤其是错误和警告).随构建的进行立即进行.)

参见:
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.io. streamreader.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.io. stream.aspx [^ ].

—SA
You can re-direct outputs of the console application into some streams you can provide by yourself. Use System.Diagnostics.Process.Start(ProcessStartInfo).

You can re-direct two output streams: System.Diagnostics.Process.StandardError and System.Diagnostics.Process.StandardOutput. You need to use System.Diagnostics.ProcessStartInfo in order to specify System.Diagnostics.ProcessStartInfo.RedirectStandardError or System.Diagnostics.ProcessStartInfo.RedirectStandardErrorRedirectStandardOutput.

You can use file stream for redirection of StandardError or StandardOutput or some other stream, as these properties should of the type System.IO.StreamReader. You can create your own class derived from System.IO.StreamReader or use its constructor accepting the parameter of the abstract class System.IO.Stream, so you can also use your own stream class derived from System.IO.Stream. In this way, you can implement any stream type to your requirements to be used for stream re-direction.

(One typical example of this re-direction feature is running a compiler or а linker (or other tool used for a build step) under IDE, where a compiler or а linker output (errors and warnings, in particular) is displayed in IDE control immediately as the build goes.)

See:
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.io.streamreader.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.stream.aspx[^].

—SA


这篇关于如何使用缓冲区(管道)读取控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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