2008-10-11 42 views

回答

17
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

编辑: 如果你需要得到你需要使用异步操作的错误消息。您可以使用异步读取操作来避免这些依赖关系及其潜在的死锁。或者,您可以通过创建两个线程并在单独的线程中读取每个流的输出来避免死锁情况。

+1

你也想要启用“RedirectStandardError”,并且读取该流, 如果你的子进程生成错误消息。 – gimel 2008-10-11 15:01:00

相关问题