Sava Filipovic Sava Filipovic - 1 year ago 148
C# Question

Execute python 3 code from c# forms application

As a school project my class made a simple programing language in python 3. Now we made a simple ide in c# that should execute python script in a new console window. I was wondering what is most efficient way to do it. (I should execute it with parameters)

Answer Source

You can use ProcessStartInfo

int parameter1 = 10;
int parameter2  = 5
Process p = new Process(); // create process to run the python program
p.StartInfo.FileName = "python.exe"; //Python.exe location
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false; // ensures you can read stdout
p.StartInfo.Arguments = "c:\\src\\ "+parameter1 +" "+parameter2; // start the python program with two parameters
p.Start(); // start the process (the python program)
StreamReader s = p.StandardOutput;
String output = s.ReadToEnd();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download