2013-05-28 88 views
0

在我的案例中有一個過程開始另一個過程並訂閱Exited事件。 最後的第二個進程應該以某種方式將錯誤字符串傳遞給第一個進程。第一個進程將在屏幕上顯示消息。如何將字符串從一個進程傳遞到另一個進程?

如何在應用程序的最後傳遞一個字符串,以便另一個應用程序能夠讀取該字符串並顯示它?

+3

使用文件可能是最簡單的方法。 – David

+0

或者一個隊列。也許MSMQ。 – davenewza

回答

2

您可以重定向StandardError的流:http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx

Process myProcess = new Process(); 
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("net ","use "+ args[0]); 

myProcessStartInfo.UseShellExecute = false; 
myProcessStartInfo.RedirectStandardError = true; 
myProcess.StartInfo = myProcessStartInfo; 
myProcess.Start(); 

StreamReader myStreamReader = myProcess.StandardError; 
// Read the standard error of net.exe and write it on to console. 
Console.WriteLine(myStreamReader.ReadLine()); 
myProcess.Close(); 

以下研究,並挑選符合您需求的最適合您的方案之一。我的猜測是,如果你將數據傳遞迴父進程,你需要輸出或錯誤。

StartInfo.RedirectStandardOutput 
StartInfo.RedirectStandardError 
StartInfo.RedirectStandardInput 
+0

以及my​​Process如何傳達錯誤?只需要將字符串寫入控制檯? – EngineerSpock

+0

您可以重定向不僅僅是錯誤,如果這不適合你。 –

相關問題