2013-03-06 131 views
0

我正在爲閉包編譯器編寫一個包裝類,並且我得到了空字符串process.StandardOutput.ReadToEnd()我寫了下面的代碼。通過process.StandardOutput.ReadToEnd()返回空白輸出

public class ClosureCompiler 
    { 
     ProcessStartInfo psi = new ProcessStartInfo(); 
     string _commandpath; 
     public ClosureCompiler(string commandpath) 
     { 
      _commandpath = commandpath; 

      psi.FileName = "java.exe"; 

      psi.UseShellExecute = false; 
      psi.RedirectStandardOutput = true; 
     } 

     public string Compile(string sourcefile) 
     { 
      psi.Arguments = " -jar " + _commandpath + " --js " + sourcefile; // +" --js_output_file " + destinationfile + ""; 

      var process = Process.Start(psi); 

      process.WaitForExit(); 
      return process.StandardOutput.ReadToEnd(); 
     } 
    } 

但是當我從標準輸出顯示的命令行輸出運行命令。

回答

0

更改行的順序process.WaitForExit();和process.StandardOutput.ReadToEnd(); WaitForExit完成後,process.StandardOutput已經「死」。

你的代碼(方法編譯)應該是這樣的:

var process = Process.Start(psi); 
string stdOutput = process.StandardOutput.ReadToEnd(); 
process.WaitForExit(); 
return stdOutput; 

您也可以註冊一個委託與Process.OutputDataReceived事件接收輸出數據