0

I'm making a mini Python IDE for fun. Why not. So I want to be able to call a python script from C# and right now I'm just testing a simple scenario. I know this is NOT how professional IDE's probably work.

    private void Run_Click(object sender, EventArgs e)
    {
        run_cmd("C:/Python34/python.exe", "C:/Users/Alaseel/Desktop/test.py");
    }

    private void About_Click(object sender, EventArgs e)
    {
        // Open the about documentation
    }

    private void run_cmd(string cmd, string args)
    {
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = "C:/Python34/python.exe";
        start.Arguments = string.Format("{0} {1}", cmd, args);
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                Console.Write(result);
            }
        }
    }

Whenever I click the "run" button on the Windows Form app, it briefly runs the python.exe then closes. It does not actually run the file that I passed in. Am I doing something wrong?

PS: The run_cmd method is NOT mine. I looked up this issue previously on a thread and used their code. But I think I'm using the method wrong.

Any ideas? Thank you!

1 Answer 1

3

You are actually putting twice your python.exe path in this case. You have it as cmd and start.Filename

Your commandline will look like : "C:/Python34/python.exe" "C:/Python34/python.exe" "C:/Users/Alaseel/Desktop/test.py"

Which is probably an invalid command.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help! How would I restructure the method signature or the method call?
There are different ways to do this. I would probably do something like : start.FileName = cmd. And then, you don't pass cmd to Arguments, as..it is not an argument to the program you are calling but the program itself ! So, start.Arguments = args. That should work ! ( I didn't test )

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.