当我尝试运行此代码时,我不断收到错误System.ComponentModel.Win32Exception: The specified executable is not a valid application for this OS platform.
。
似乎myProcess.Start()
行是问题所在。
谁能帮助我。
string python = @"C:\Users\mk\Desktop\A.py";
// python app to call
string myPythonApp = "sum.py";
// dummy parameters to send Python script
int x = 2;
int y = 5;
// Create new process start info
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);
// make sure we can read the output from stdout
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
// start python app with 3 arguments
// 1st arguments is pointer to itself,
// 2nd and 3rd are actual arguments we want to send
myProcessStartInfo.Arguments = myPythonApp + " " + x + " " + y;
Process myProcess = new Process();
// assign start information to the process
myProcess.StartInfo = myProcessStartInfo;
Console.WriteLine("Calling Python script with arguments {0} and {1}", x, y);
// start the process
myProcess.Start();
}
}
}
答案 0 :(得分:1)
您试图将A.py
作为可执行文件(例如.exe
)调用,但不是(只是文本,不是程序集)。
相反,您可以通过CMD调用Python以运行A.py
,并将sum.py
作为附加参数传递。
这是一个例子:
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("cmd.exe");
// run python from console.
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.Arguments = "/C python A.py " + myPythonApp + " " + x + " " + y;
Process myProcess = new Process();
myProcess.StartInfo = myProcessStartInfo;
// start the process
myProcess.Start();