由process.StandardOutput.ReadToEnd()返回的空白输出

时间:2013-03-06 07:49:22

标签: c# process

我正在为闭包编译器编写一个包装类,我正在通过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();
        }
    }

但是当我从标准输出上显示的命令行输出运行命令时。

1 个答案:

答案 0 :(得分: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事件

的输出数据