我必须使用其他应用程序(控制台)将某些参数传递给该程序,然后在我的C#程序中获取该程序的输出。我不想看到控制台(用户看不见)。我怎样才能做到这一点?

最佳答案

Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("YOUPROGRAM_CONSOLE.exe" );
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();

StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadLine();
Console.WriteLine(myString);
myProcess.Close();

资料来源:MSDN

编辑:
如果您需要获取错误消息,则需要使用异步操作。您可以使用异步读取操作来避免这些依赖性及其潜在的死锁。另外,您可以通过创建两个线程并在单独的线程上读取每个流的输出来避免死锁情况。

10-05 19:57