1
我想從C#調用Python腳本,到目前爲止這麼好。但是,當我嘗試調用這個特定的腳本時,它不能工作。這是我在C#我做:從C#進程調用特定的Python腳本
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "C:\\Python27\\python.exe";
startInfo.Arguments = "pyScript.py";
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
StreamReader output_sr = exeProcess.StandardOutput;
StreamReader error_sr = exeProcess.StandardError;
String output = output_sr.ReadToEnd();
String error = error_sr.ReadToEnd();
Console.WriteLine(output);
Console.WriteLine(error);
exeProcess.WaitForExit();
}
我pyScript.py文件如下:
with open ('test.txt','w') as f:
for i in range(0x34):
f.write('1')
f.close()
所以基本上我SPECT是創建一個名爲包含「的test.txt」文件:
1111111111111111111111111111111111111111111111111111
而且我沒有得到任何錯誤,如果我從控制檯運行它,它的作品,即使我雙擊我的Python腳本,它的工作原理。這真的很奇怪,我不知道爲什麼當我打電話,如果從C#文件沒有被生成。
只是好奇,是否有任何理由你沒有使用IronPython? – 2014-10-03 19:01:29
是的,這是一個基本的例子,但我的項目是調用一個巨大的已經開發的Python指令集與USB通信交互,使用IronPython會導致我更多的兼容性問題和更多的工作,而不僅僅是作爲一個進程C#。 – Israboy 2014-10-03 19:03:32
真的嗎?我假設IronPython與常規Python有很好的兼容性。我很抱歉。我現在沒有什麼可去的了。對不起:( – 2014-10-03 19:09:11