0

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

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

4

1 回答 1

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

于 2013-03-06T08:40:22.840 回答