StandardError显示,但在C#中调用Process时不会出现StandardOutput

时间:2017-08-10 09:45:54

标签: c# python process invalidoperationexception redirectstandardoutput

下面是对Python进程的调用,但它不返回StandardOutput。我专心在Python文件中输入了一个拼写错误,并成功返回StandardError

        string pythonExe = @"../../../Vokaturi/python.exe";

        string pythonFile = @"../../../Vokaturi/test.py";

        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(pythonExe);

        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;
        myProcessStartInfo.RedirectStandardError = true;

        myProcessStartInfo.Arguments = pythonFile;

        Process myProcess = new Process();
        myProcess.StartInfo = myProcessStartInfo;
        myProcess.Start();

        StreamReader myStreamReaderError = myProcess.StandardError;
        string myErrorString = myStreamReaderError.ReadToEnd();

        StreamReader myStreamReaderOutput = myProcess.StandardError;
        string myOutputString = myStreamReaderOutput.ReadToEnd();
        myProcess.WaitForExit();
        myProcess.Close();

这意味着我在路径中调用文件的方式是正确的。我也在命令提示符下尝试了它,它返回必要的输出而没有错误。当我调试代码时,它说它抛出了system.invalidoperationexception类型的异常(在myProcess.start()之后)。

enter image description here

但它表示here是预期的。我错过了什么?提前感谢您的指导。

1 个答案:

答案 0 :(得分:0)

您只读取readeroutput的标准错误

 StreamReader myStreamReaderError = myProcess.StandardError;
    string myErrorString = myStreamReaderError.ReadToEnd();

    StreamReader myStreamReaderOutput = myProcess.StandardError;
    string myOutputString = myStreamReaderOutput.ReadToEnd();

也改变它

StreamReader myStreamReaderError = myProcess.StandardError;
    string myErrorString = myStreamReaderError.ReadToEnd();

    StreamReader myStreamReaderOutput = myProcess.StandardOutput;
    string myOutputString = myStreamReaderOutput.ReadToEnd();