1

I am new to C# and I tried to execute a python program using the ProcessStartInfo class. I followed this link and did the same. However, I cannot get any redirect output. I tried to execute the Python interpreter with the argument and there were no problems. However, I saw the console pop up and then went away immediately. There was no output print. I tried to use the method process.waitForExit() but the console also went away. Any ideas?

ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe";
            start.Arguments = @"C:\Users\test\test.py";
            start.UseShellExecute = false;
            start.RedirectStandardOutput = true;
            start.RedirectStandardError = true;
            using (Process process = Process.Start(start))
            {

                using (StreamReader reader = process.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    Console.Write(result);
                    process.WaitForExit();
                }
            }
1
  • Is it possible that it's printing out an error? Try reading from StandardError instead of StandardOutput to see if that shows anything. Commented Jun 30, 2018 at 16:26

1 Answer 1

1

If you start your application from VS then VS closes application console after finishing execution. Try to add Console.ReadLine() as the last line in your program.

using (Process process = Process.Start(start))
{
    using (StreamReader reader = process.StandardOutput)
    {
        string result = reader.ReadToEnd();
        Console.Write(result);
    }
 }
 Console.ReadLine();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.