【问题标题】:Capturing an c# executable output from another C# program从另一个 C# 程序捕获 C# 可执行输出
【发布时间】:2012-05-09 22:54:51
【问题描述】:

我正在执行一个 C# 程序,即来自另一个 C# 程序的 .exe。但是 .exe 在其程序中有一些 Console.WriteLine() 。我想把标准输出放到我的 C# 程序中。

例如,

考虑一个 C# 可执行文件,即 1.exe,还有另一个 Program 2.cs。

我从 2.cs 1.exe 调用。现在控制台从 1.exe 显示一些输出。但我想要我的程序 2.cs 中的输出。用于向用户显示信息。

有可能吗?请帮忙

谢谢 赛辛杜

【问题讨论】:

标签: c# .net console-application outputstream


【解决方案1】:

您可以使用 ProcessStartInfo.RedirectStandardOutput 属性

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();

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx

【讨论】:

    【解决方案2】:

    您必须重定向标准输出流,查看MSDN 了解更多信息。

    当进程将文本写入其标准流时,该文本是 通常显示在控制台上。通过重定向标准输出 流,您可以操纵或抑制进程的输出。为了 例如,您可以过滤文本、设置不同的格式或编写 输出到控制台和指定的日志文件。

    【讨论】:

      猜你喜欢
      • 2010-10-10
      • 2017-08-15
      • 1970-01-01
      • 2016-10-02
      • 2013-12-23
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2011-01-15
      相关资源
      最近更新 更多