从.NET应用程序捕获控制台输出(C#) [英] Capturing console output from a .NET application (C#)

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

问题描述

我如何从我的.NET应用程序中调用控制台应用程序,并捕获在控制台中产生的所有输出?

How do I invoke a console application from my .NET application and capture all the output generated in the console?

(记住,我不想先保存在一个文件中的信息,然后重新登录,我很乐意接受它活。)

(Remember, I don't want to save the information first in a file and then relist as I would love to receive it as live.)

推荐答案

这可以很容易地使用<一个实现href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx\">ProcessStartInfo.RedirectStandardOutput属性。一个完整的样本包含链接的MSDN文档中;唯一需要注意的是,你可能要重定向标准错误流,以及看到您的应用程序的所有输出。

This can be quite easily achieved using the ProcessStartInfo.RedirectStandardOutput property. A full sample is contained in the linked MSDN documentation; the only caveat is that you may have to redirect the standard error stream as well to see all output of your application.

Process compiler = new Process();
compiler.StartInfo.FileName = "csc.exe";
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();    

Console.WriteLine(compiler.StandardOutput.ReadToEnd());

compiler.WaitForExit();

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

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