问题描述
如何从我的 .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#) 捕获控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!