0

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!

Community
  • 1
  • 1
CircAnalyzer
  • 610
  • 1
  • 9
  • 29

3 Answers3

2

You're initializing your cmd variable incorrectly. The "@" should precede the quotation mark (i.e. @"C:/..."). Of course, using forward slashes instead of backslashes negates the need for the verbatim string anyway so you can omit the "@" altogether.

String cmd = "C:/Program Files (x86)/Python27/python.exe";
itsme86
  • 19,266
  • 4
  • 41
  • 57
1

try it

string cmd = @"C:\Program Files (x86)\Python27\python.exe";

or

string cmd = @"C:\PROGRA~1\Python27\python.exe";
Eren G.
  • 455
  • 1
  • 5
  • 14
0

read and load python exe into memory first.

var assembly = Assembly.GetExecutingAssembly();
var resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith("python.exe"));
FileStream outputFileStream = new FileStream(Path.Combine(Directory.GetCurrentDirectory(), resourcePath), FileMode.OpenOrCreate, FileAccess.ReadWrite);
using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
{
    if (stream != null)
    {
        stream.CopyTo(outputFileStream);
        stream.Close();
    }
    outputFileStream.Close();
}
Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
  • Then start the process. – Biswajit Dash Aug 05 '21 at 19:38
  • ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = Path.Combine(Directory.GetCurrentDirectory(), resourcePath); psi.Arguments = param; psi.CreateNoWindow = false; using (Process proc = Process.Start(psi)) { this.Hide(); await proc.WaitForExitAsync(); this.Close(); } – Biswajit Dash Aug 05 '21 at 19:38