捕获C#从另外一个C#程序输出的可执行文件另外一个、可执行文件、程序

2023-09-02 10:53:18 作者:别把月经当初夜↘

我执行C#程序即可以从另一C#程序中的.exe文件。但该.exe有一定的Console.WriteLine()的方案。我想要得到的标准输出到我的C#程序。

I am executing a C# program i.e a .exe from another C# program. but the .exe has some Console.WriteLine() in its program. I want to get the standard output into my C# program.

例如,

考虑一个C#可执行即1.EXE,有另一个计划2.cs。

Consider a C# executable i.e 1.exe and there is another Program 2.cs.

我是从2.cs 1.EXE调用。现在有在控制台从1 exe文件显示一些输出。但我想在我的程序2.cs.的输出中用于显示信息给用户。

I am calling from 2.cs 1.exe. Now there is some output that the console is displaying from 1. exe. But I want the ouput in my program 2.cs. for displaying information to user.

这可能吗?请帮忙

谢谢 西娜斯迪胡

推荐答案

您可以使用ProcessStartInfo.RedirectStandardOutput地产

You can use ProcessStartInfo.RedirectStandardOutput Property

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