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

(请记住,我不想先将信息保存在文件中,然后再重新列出,因为我希望以实时方式接收信息.)

推荐答案

使用ProcessStartInfo.RedirectStandardOutput属性可以很容易地实现这一点.完整样本包含在链接的MSDN文档中;唯一需要注意的是,您可能还必须重定向标准错误流,才能查看应用程序的所有输出.

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相关问答推荐

无法对.NET MAUI类库进行单元测试

为什么 ULong > 16 位的数学会变得不稳定?

线程安全性的单元测试?

我在哪里可以获得线程安全的 CollectionView?

.gitignore 和 Visual Studio 项目:忽略 bin/Debug 目录但不忽略 bin/Release 目录

在一个 LINQ 查询中获取两列的总和

IIS Express - 500.19 无法读取配置文件 - 因为它正在查看错误的路径

编译错误:显式实现接口时修饰符 'public' 对此项目无效

为什么 ?: 会导致转换错误,而 if-else 不会?

如何在我的 C# 程序的面板中运行另一个应用程序?

在 .NET 中计算目录大小的最佳方法是什么?

监听依赖属性的变化

仅使用 XAML 绘制纯色三角形

ToLowerInvariant() 有什么问题?

Environment.GetFolderPath(...CommonApplicationData) 在 Vista 上仍然返回C:\Documents and Settings\

任何人都知道缺少枚举通用约束的好方法吗?

从 Visual Studio 2015 发布 - 允许不受信任的证书

如何为 Dapper 查询动态创建参数

App.config:用户与应用程序范围

什么时候使用 Tuple 和 KeyValuePair 比较好?