How does a shell execute a file in C #?

I tried to use the Process class, as always, but that didn't work. All I do is try to run a Python file as someone double-clicked it.

Is it possible?

EDIT:

Code example:

string pythonScript = @"C:\callme.py";

string workDir = System.IO.Path.GetDirectoryName ( pythonScript );

Process proc = new Process ( );
proc.StartInfo.WorkingDirectory = workDir;
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = pythonScript;
proc.StartInfo.Arguments = "1, 2, 3";

I am not getting any errors, but the script is not running. When I run the script manually, I see the result.

+5
source share
3 answers

Here is my code to execute a python script from C # with redirected standard input and output (I pass the information through standard input), copied from an example on the Internet somewhere. The Python location is hard-coded, as you can see, refactoring can.

    private static string CallPython(string script, string pyArgs, string workingDirectory, string[] standardInput)
    {

        ProcessStartInfo startInfo;
        Process process;

        string ret = "";
        try
        {

            startInfo = new ProcessStartInfo(@"c:\python25\python.exe");
            startInfo.WorkingDirectory = workingDirectory;
            if (pyArgs.Length != 0)
                startInfo.Arguments = script + " " + pyArgs;
            else
                startInfo.Arguments = script;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardInput = true;

            process = new Process();
            process.StartInfo = startInfo;


            process.Start();

            // write to standard input
            foreach (string si in standardInput)
            {
                process.StandardInput.WriteLine(si);
            }

            string s;
            while ((s = process.StandardError.ReadLine()) != null)
            {
                ret += s;
                throw new System.Exception(ret);
            }

            while ((s = process.StandardOutput.ReadLine()) != null)
            {
                ret += s;
            }

            return ret;

        }
        catch (System.Exception ex)
        {
            string problem = ex.Message;
            return problem;
        }

    }
+7
source
+5

You forgot proc.Start () at the end. The code you have should work if you call Start ().

+3
source

All Articles