C#程序调用Python脚本失败求助:参数未执行且输入重定向报错
C#运行Python脚本并获取执行结果的问题解决方法
问题背景
需求是通过C#程序运行Python脚本并查看执行结果,但现有代码存在以下问题:
- 给
cmd.exe传递Arguments参数后,命令未执行 - 尝试用
StandardInput.WriteLine发送命令时,提示“StandardIn has not been redirected”,即使设置UseShellExecute=false也无效 - 手动在命令提示符中输入对应命令可正常运行脚本
错误原因
cmd.exe参数格式错误:直接给cmd.exe传命令参数时,必须加上/c参数(表示执行命令后退出),否则cmd只会打开窗口而不执行后续命令- 标准输入未正确重定向:使用
StandardInput前,必须显式设置RedirectStandardInput = true,仅设置UseShellExecute=false不足以启用标准输入重定向 - 冗余的
cmd.exe调用:直接调用Python.exe比通过cmd.exe中转更高效,也能避免cmd的参数解析问题
修正方案
方案1:直接调用Python.exe(推荐)
跳过cmd.exe,直接启动Python进程执行脚本,同时重定向输出以获取结果:
private void RunPythonScript(string pathPythonExe, string pathPyFolder, string SN) { using (Process p = new Process()) { p.StartInfo.FileName = pathPythonExe; p.StartInfo.WorkingDirectory = pathPyFolder; // 传递脚本路径和命令行参数 p.StartInfo.Arguments = $"Z19003.py --sn {SN}"; p.StartInfo.UseShellExecute = false; // 重定向标准输出和错误输出,用于获取执行结果 p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.Start(); // 读取所有输出和错误信息 string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); // 等待进程执行完成 p.WaitForExit(); // 处理执行结果(可根据需求替换为日志、界面展示等) Console.WriteLine("脚本执行输出:"); Console.WriteLine(output); if (!string.IsNullOrEmpty(error)) { Console.WriteLine("脚本执行错误:"); Console.WriteLine(error); } } }
方案2:通过cmd.exe调用(若必须使用cmd)
如果需要通过cmd.exe执行命令,需添加/c参数并正确格式化命令:
private void RunViaCmd(string pathPythonExe, string pathPyFolder, string SN) { using (Process p = new Process()) { p.StartInfo.FileName = "C:\\Windows\\system32\\cmd.exe"; p.StartInfo.WorkingDirectory = pathPyFolder; // /c 参数告诉cmd执行完命令后退出,命令需用引号包裹避免路径含空格时出错 p.StartInfo.Arguments = $"/c \"{pathPythonExe} Z19003.py --sn {SN}\""; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.Start(); string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); p.WaitForExit(); Console.WriteLine("执行输出:"); Console.WriteLine(output); if (!string.IsNullOrEmpty(error)) { Console.WriteLine("执行错误:"); Console.WriteLine(error); } } }
方案3:使用StandardInput发送命令(解决重定向错误)
若一定要通过StandardInput传递命令,需显式开启标准输入重定向:
private void RunWithStandardInput(string pathPythonExe, string pathPyFolder, string SN) { using (Process p = new Process()) { p.StartInfo.FileName = "C:\\Windows\\system32\\cmd.exe"; p.StartInfo.WorkingDirectory = pathPyFolder; p.StartInfo.UseShellExecute = false; // 必须同时开启三个重定向才能使用StandardInput p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.Start(); // 发送命令到cmd窗口 p.StandardInput.WriteLine($"{pathPythonExe} Z19003.py --sn {SN}"); // 关闭输入流,告知cmd没有更多命令 p.StandardInput.Close(); string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); p.WaitForExit(); Console.WriteLine("执行输出:"); Console.WriteLine(output); if (!string.IsNullOrEmpty(error)) { Console.WriteLine("执行错误:"); Console.WriteLine(error); } } }
关键注意事项
- 使用
using语句包裹Process对象,确保进程资源被正确释放 - 若Python路径或脚本路径包含空格,需用引号包裹对应路径(方案2中已处理)
WaitForExit()会阻塞当前线程,若需异步执行可使用Exited事件
内容的提问来源于stack exchange,提问作者Bosskardo
相关产品推荐
相关产品推荐

