How do I run a Python script from C#?

后端 未结 8 1848
猫巷女王i
猫巷女王i 2020-11-22 02:47

This sort of question has been asked before in varying degrees, but I feel it has not been answered in a concise way and so I ask it again.

I want to run a script in

8条回答
  •  执笔经年
    2020-11-22 03:17

    Set WorkingDirectory or specify the full path of the python script in the Argument

    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = "C:\\Python27\\python.exe";
    //start.WorkingDirectory = @"D:\script";
    start.Arguments = string.Format("D:\\script\\test.py -a {0} -b {1} ", "some param", "some other param");
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;
    using (Process process = Process.Start(start))
    {
        using (StreamReader reader = process.StandardOutput)
        {
            string result = reader.ReadToEnd();
            Console.Write(result);
        }
    }
    

提交回复
热议问题