您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何从C#运行Python脚本?

如何从C#运行Python脚本?

它不起作用的原因是因为你有UseShellExecute = false

如果不使用外壳程序,则必须以方式提供python可执行文件的完整路径FileName,并构建Arguments字符串以提供脚本和要读取的文件

另请注意,RedirectStandardOutput除非你不能这样做UseShellExecute = false

我不太确定应如何为python格式化参数字符串,但你将需要以下内容

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);
         }
     }
}
python 2022/1/1 18:24:19 有166人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶