码迷,mamicode.com
首页 > 编程语言 > 详细

How do I run a Python script from C#?

时间:2021-05-24 01:04:39      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:private   reader   str   RoCE   ade   inf   rom   The   standard   

How do I run a Python script from C#?

The reason it isn‘t working is because you have UseShellExecute = false.

If you don‘t use the shell, you will have to supply the complete path to the python executable as FileName, and build the Arguments string to supply both your script and the file you want to read.

Also note, that you can‘t RedirectStandardOutput unless UseShellExecute = false.

I‘m not quite sure how the argument string should be formatted for python, but you will need something like this:

private void run_cmd(string cmd, string args)
{
     ProcessStartInfo start = new ProcessStartInfo();
     start.FileName = "my/full/path/to/python.exe";
     start.Arguments = string.Format("{0} {1}", cmd, 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);
         }
     }
}

 

How do I run a Python script from C#?

标签:private   reader   str   RoCE   ade   inf   rom   The   standard   

原文地址:https://www.cnblogs.com/chucklu/p/14743293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!