c#进程运行python脚本



c#进程运行python脚本

我尝试使用c#运行python文件,但它没有运行

my error is

System.ComponentModel。Win32Exception (0x80004005):指定的异常可执行文件不是一个有效的应用程序。

在System.Diagnostics.Process.StartWithCreateProcess (ProcessStartInfostartInfo)

这是我的代码

try
{
Process cmd = new Process();
string filePath = currentDirectory;
string pingcmd = "  " + argument1 + " " + argument2 + "   ";
cmd.StartInfo.WorkingDirectory = currentDirectory;
cmd.StartInfo.FileName = Directory.GetCurrentDirectory() + "/xx.py";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(708);
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.Arguments = pingcmd;
cmd.Start();
string allData = "";
int i = 0;
while (!cmd.StandardOutput.EndOfStream)
{
string line = cmd.StandardOutput.ReadLine();
MessageBox.Show(line);             
}
cmd.WaitForExit();

}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}

我找到了很好的代码来做这件事'

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);
}
}
}

相关内容

  • 没有找到相关文章

最新更新