2014-10-01 96 views
4

此代碼的工作我的測試系統(原Windows的服務器的複製2008 R2)過程WaitForExit()永遠不會結束(CMD openFiles散)

private string _getNetFiles() 
{ 
    // prepare execution process 
    ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe", "/c openfiles /query /Fo list"); 
    processStartInfo.CreateNoWindow = true; 
    processStartInfo.UseShellExecute = false; 
    processStartInfo.RedirectStandardError = true; 
    processStartInfo.StandardOutputEncoding = System.Text.Encoding.GetEncoding(437); 
    processStartInfo.RedirectStandardOutput = true; 

    // execute 
    Process process = Process.Start(processStartInfo); 
    process.WaitForExit(); 


    // read outputs 
    string stdOutput = process.StandardOutput.ReadToEnd(); 
    string stdError = process.StandardError.ReadToEnd(); 

    return stdOutput; 
} 

在原有系統上的罰款: 我看到「 cmd.exe/c openfiles/query/Fo list「Task-Manger中的任務,但這個任務永遠不會結束(process.WaitForExit()進程永遠不會結束)。 Cmd原系統:openfiles/query/fo列表作品還行!

問題在哪裏?

問候raiserle

編輯: 我可以停止使用任務管理器的進程。 stdOutput是正確的。爲什麼不結束cmd-taks。

回答

4

子進程正在等待輸入或讀取其輸出。管道緩衝區不是無限大。您需要不斷排出標準輸出以及標準錯誤。

Get Values from Process StandardOutput看起來很合理。 https://stackoverflow.com/a/24084220/122718文件如何安全讀取兩個流。

另請注意Visual Basic Capture output of cmd以及everything that Hans Passant says on this topic

在沒有輸出重定向的情況下使用Process類相當棘手,記錄不當。

+0

任何建議,爲什麼在測試系統上工作?錯誤(讀取stderr結束)不在測試系統上。任務沒有阻塞。 – raiserle 2014-10-01 17:57:27

+0

我對這個命令的作用以及在什麼情況下可能與輸入有關沒有具體的瞭解。 – usr 2014-10-02 10:15:22

相關問題