2008-10-11 104 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

相關問題