2010-09-03 138 views

回答

0

通過redirecting it並讀取流。

+6

-1:鏈接文章遇到了一個死鎖問題(至少在編寫本文時): 正如MSDN文檔所述:「死鎖條件結果如果父節點ocess調用p.StandardOutput.ReadToEnd後跟p.StandardError.ReadToEnd並且子進程寫入足夠的文本以填充其錯誤流「。 – tstone2077 2014-09-04 20:03:07

-1

示例代碼如下:

 ProcessStartInfo psi = new ProcessStartInfo(); 
     psi.CreateNoWindow = false; 
     psi.UseShellExecute = false; 
     psi.FileName = "C:\\my.exe"; 
     psi.WindowStyle = ProcessWindowStyle.Hidden; 
     psi.RedirectStandardOutput = true; 
     psi.RedirectStandardError = true; 

     using (Process exeProcess = Process.Start(psi)) 
     { 
      exeProcess.WaitForExit(); 

      var exitCode = exeProcess.ExitCode; 
      var output = exeProcess.StandardOutput.ReadToEnd(); 
      var error = exeProcess.StandardError.ReadToEnd(); 

      if (output.Length > 0) 
      { 
       // you have some output 
      } 


      if(error.Length > 0) 
      { 
       // you have some error details 
      } 
     } 
+1

您需要將UseShellExecute設置爲true才能遵守Verb,並且必須將其設置爲'false'才能重定向標準輸出。你不能這樣做。 我敢肯定,Windows也不允許你跨管理/非管理安全邊界重定向標準輸入/輸出/錯誤。你必須找到一種不同的方式來獲得以管理員身份運行的程序的輸出 - 參考:http://stackoverflow.com/a/8690661 – Kiquenet 2014-08-28 06:35:03

+5

-1。正如MSDN文檔所述:「如果父進程調用p.StandardOutput.ReadToEnd,然後是p.StandardError.ReadToEnd並且子進程寫入足夠的文本以填充其錯誤流,則會導致死鎖條件」。你的代碼確實如此。 – tstone2077 2014-09-04 20:01:21

相關問題