0

Below is a call to a Python process but it doesn't return the StandardOutput. I intently made a typo inside the Python file and it successfully returned the 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();

That means the way I call the files in their paths are correct. I also tried it in command prompt and it returns the necessary output without error. When I debug the code, it says it threw an exception of type system.invalidoperationexception (after myProcess.start()).

enter image description here

But it says here that it's expected. What am I missing? Thank you in advance for your guidance.

Judah Endymion
  • 67
  • 1
  • 12

1 Answers1

0

You are reading only standard error for the readeroutput too

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

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

change it too

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

    StreamReader myStreamReaderOutput = myProcess.StandardOutput;
    string myOutputString = myStreamReaderOutput.ReadToEnd();
npo
  • 1,060
  • 8
  • 9