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

查看:135
本文介绍了从.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.)

推荐答案

这可以使用 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天全站免登陆