2008-11-19 83 views
0

我正在編寫一個windows服務,它將用於監視一些其他服務並在服務關閉時運行外部應用程序。運行外部應用程序的.NET Windows服務

我已經得到了大部分工作,Windows服務運行,代碼監視其他服務工程,但我遇到問題,當出現故障時運行外部應用程序。

外部應用程序僅僅是一個控制檯應用程序這一串命令行開關的,但我不是100%肯定,如果開關設置正確或什麼,所以我最好一)喜歡看什麼樣的命令正在執行並且b)查看來自它的任何輸出消息。

我有這樣的代碼:

Process p = new Process(); 
p.StartInfo.FileName = "C:\MyExternalApp.exe"; 
p.StartInfo.Arguments = "stop"; 
p.Start(); 
p.WaitForExit(); 

因此,通過和我沒有任何異常拋出,但我不知道,也不如果外部應用程序沒有工作,我可以運行所有看到它的任何消息。

如何查看應用程序的輸出?

回答

1

Process類具有StandardOutput屬性 - 它是表示過程輸出的流。

從MSDN文檔:

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

StreamReader myStreamReader = myProcess.StandardOutput; 
// Read the standard output of the spawned process. 
string myString = myStreamReader.ReadLine(); 
Console.WriteLine(myString); 
myProcess.Close(); 
+0

你可能想的過程開始前將其StreamReader的?沒有? – stephbu 2008-11-19 06:16:37