2

Sorry if this is something obvious, I'm super new to C#. I'm working on a program that runs a python script to check something online, that then writes it back to a file that C# reads and puts on a form. Everything works if I manually run them both at the same time, but I really want to start the script from the C# program.

Here's the function that should start the python script:

private void RunPython()
{
        Process p = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = "python " + path + "\\foo.py";
        p.StartInfo = startInfo;
        p.Start();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
        // get things set up... etc.
        RunPython();
}

I've tried running it without the window hidden, and it just brings up an empty cmd line in the 'path' directory. Is it never running the python script? It doesn't seem like it is, but it may also be running it and immediately closing it. I can't tell.

I need it to stay open for the duration of the C# program's run, so if the problem is it exiting when the RunPython() method returns, is there some different way I could go about this?

Thanks!!

2
  • Could you post foo.py? Commented May 31, 2017 at 20:50
  • It's like 500 lines, and it works when I start it from the command line manually. If I separately start both programs, the whole thing works, I'm just trying to find a way to only have to start one Commented Jun 1, 2017 at 13:34

1 Answer 1

1

If you want to run a program in the command line using arguments to cmd.exe you need the /c flag. Alternatively you can use /k but that keeps the command process running which you probably don't want.

/c Carries out the command specified by String and then stops.

So it's usage is cmd.exe /c [string]

Try changing your arguments line to:

startInfo.Arguments = "/c \"python " + path + "\\foo.py\"";

See here for more information about running cmd.exe and dealing with quotes in the string section of the command: https://technet.microsoft.com/en-us/library/cc771320(v=ws.11).aspx

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

5 Comments

i tried both "\"python " + path + "\\foo.py\""; and "/c \"python " + path + "\\foo.py\""; and neither worked. When I make the window style visible it still just shows an empty line in the path directory, but no indications it started the python file.
Wait. This worked. There was another problem with the path I was giving it. This answers the question. Thank you! Here's the working code: startInfo.Arguments = "/c \"python foo.py\"";
@Zaya Sorry about that, I wrote out the command arguments with the quotes but forgot the flag itself. Glad you figured it out and I edited the question so it should be correct now.
Can you post an example with python script args?
@Learner You should just be able to add them to the arguments string between foo.py and the \" that closes the python argument string.

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.