0
我正在製作一個迷你Python IDE以獲得樂趣。爲什麼不。所以我想能夠從C#調用python腳本,現在我只是測試一個簡單的場景。我知道這不是專業IDE可能的工作方式。試圖在C中調用Python腳本#
private void Run_Click(object sender, EventArgs e)
{
run_cmd("C:/Python34/python.exe", "C:/Users/Alaseel/Desktop/test.py");
}
private void About_Click(object sender, EventArgs e)
{
// Open the about documentation
}
private void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:/Python34/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);
}
}
}
每當我點擊Windows窗體應用程序上的「運行」按鈕,它會短暫運行python.exe,然後關閉。它實際上並沒有運行我傳入的文件。我做錯了什麼?
PS:run_cmd方法不是我的。我之前在一個線程中查找過這個問題,並使用了他們的代碼。但我認爲我使用的方法錯了。
任何想法?謝謝!
感謝您的幫助!我將如何重構方法簽名或方法調用? – katie1245
有不同的方法來做到這一點。我可能會做這樣的事情:start.FileName = cmd。然後,您不會將cmd傳遞給參數,因爲它不是您正在調用的程序的參數,而是程序本身!所以,start.Arguments = args。這應該工作! (我沒有測試) – HammerHeart