2013-03-06 552 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事件接收输出数据