I am trying to execute a python script from C#. I found a way to accomplish this in another post at the following link: run a python script from c#. However, I am getting an error when the process arrives to the using (Process process = Process.Start(start))
line. Here is the code that I am implementing:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RunPy
{
class Program
{
static void Main(string[] args)
{
// As an example, cmd would be @C:/Python26/python.exe and args would be C://Python26//test.py 100
String cmd = "@C:/Program Files (x86)/Python27/python.exe";
String argss = "C:/My_Python_lib/happyBirthday.py 'joe'";
// Console.Write(argss); this line is just to test the output of the above argss
// Console.ReadLine(); this line goes with the above line to prevent the window from closing so fast
run_cmd(cmd, argss);
//ProcessStartInfo startInfo = new ProcessStartInfo();
//startInfo.FileName = cmd;
//Process.Start(cmd);
}
private static void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = cmd;
start.Arguments = 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);
}
}
}
}
}
Here is the error that I am getting back:
An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
Any help would be appreciated. Thanks!