2013-06-28 11 views
2

我正在運行一個命令行實用程序使用Process.Start。爲了調試目的,我想將其輸出流傳送到Visual Studio的調試輸出面板(Debug.Write)。我希望實時做到這一點,而不是等待過程完成,然後一次寫完。管流到Debug.Write()

我知道這在理論上是可行的,但我沒有足夠的經驗與Stream對象知道如何做到這一點。

回答

3

這可能不是你想要的,但它讓你走上了正確的軌道,我想。

p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.OutputDataReceived += p_OutputDataReceived; 
p.Start(); 
p.BeginOutputReadLine(); 

然後,你的事件處理程序接收數據。

void p_OutputDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    Debug.Write(e.Data); 
} 
+1

謝謝先生 - 就是這樣。只需要注意一點,你不能使用這種技術並且'p.StandardOuptut.ReadToEnd()'你必須創建一個StringBuilder並在事件處理器中自己構建它。很簡單,只需注意一些事情。 –