2011-07-11 188 views
1

我有一個控制檯應用程序,它執行一組操作並在完成每個操作後發出消息。當我運行我的控制檯應用程序,在我的控制檯窗口中的消息可能是這樣的:從控制檯窗口輸入到Winform應用程序

Checking prerequisites... 
Completing prerequisites.. 
Performing installation... 
Completing installation... 
Done..! 

現在,我從我的C#Windows應用程序一個使用Process.StartInfo執行此控制檯應用程序()。 我需要將我的控制檯應用程序拋出的所有消息顯示在我的應用程序的窗口窗體中。

可以這樣做嗎?

謝謝。

+0

的可能重複的[捕獲在.NET控制檯輸出(C#)](http://stackoverflow.com/questions/186822/capturing-the-console-output-in-net-c) – RvdK

回答

1

這可以通過使用ProcessStartInfo.RedirectStandardOutput屬性是相當容易實現的樣子。完整的示例包含在鏈接的MSDN documentation中。

Process compiler = new Process(); 
compiler.StartInfo.FileName = "csc.exe"; 
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs"; 
compiler.StartInfo.UseShellExecute = false; 
compiler.StartInfo.RedirectStandardOutput = true; 
compiler.Start();  

Console.WriteLine(compiler.StandardOutput.ReadToEnd()); 

compiler.WaitForExit(); 
相關問題