作为一个学校项目,我的课程在python 3中制作了一个简单的编程语言。现在我们在c#中做了一个简单的ide,它应该在新的控制台窗口中执行python脚本。我想知道什么是最有效的方法。 (我应该用参数来执行)从c#窗体应用程序执行python 3代码
回答
您可以使用ProcessStartInfo
int parameter1 = 10;
int parameter2 = 5
Process p = new Process(); // create process to run the python program
p.StartInfo.FileName = "python.exe"; //Python.exe location
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false; // ensures you can read stdout
p.StartInfo.Arguments = "c:\\src\\yourpythonscript.py "+parameter1 +" "+parameter2; // start the python program with two parameters
p.Start(); // start the process (the python program)
StreamReader s = p.StandardOutput;
String output = s.ReadToEnd();
Console.WriteLine(output);
p.WaitForExit();
我试过这个和consloe打开和关闭没有任何输出 –
让我调查 – Jaco
我在这里上传了项目https://www.mediafire.com/?81ppkgl5fpel595 –
有运行python脚本双向:
- 一种方法是运行Python脚本通过运行python.exe文件:
使用ProcessStartInfo运行python.exe文件并在其上传递python脚本。
私人无效RUN_CMD(串CMD,串参数){
的ProcessStartInfo开始=新的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); } } }
- 的另一种方式是使用IronPython的和直接执行Python脚本文件。
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;private static void doPython() { ScriptEngine engine = Python.CreateEngine(); engine.ExecuteFile(@"test.py"); }
- 1. 从C#代码执行外部程序
- 2. 从python程序执行c程序
- 3. C#/ Windows窗体:窗体的代码不被执行
- 4. c#窗体窗体应用程序
- 5. C#窗体窗体应用程序
- 6. C#代理窗体窗体应用程序
- 7. Android应用程序中的C/C++代码如何执行?
- 8. 从Windows执行批处理文件窗体应用程序
- 9. 从Windows窗体应用程序退出代码
- 10. 如何从C#窗体表单应用程序执行.bat文件?
- 11. 在执行主窗口窗体应用程序类之前执行其他类
- 12. C#从服务(和Vista中)运行Windows窗体应用程序
- 13. 从C#执行excel应用程序
- 14. Windows窗体代码没有执行?
- 15. 窗体中的代码执行停止
- 16. c#窗体窗体应用程序窗体问题
- 17. 如何在WinRT C#应用程序中执行Javascript代码?
- 18. 如何轻松在C#窗体窗体应用程序中密码密码?
- 19. 当Console.ReadLine()执行时,C#控制台应用程序执行代码
- 20. 在另一个应用程序域中执行代码的C#代码?
- 21. 从python脚本执行c#console应用程序 - 好的设计?
- 22. 在窗体中执行代码加载pyside和python
- 23. Mergesort C++ Windows窗体应用程序
- 24. 多个C#窗体窗体应用程序
- 25. 在C#窗体窗体应用程序中找不到System.Windows.Input.Mouse
- 26. Windows窗体应用程序
- 27. WinForms - 如何从WebBrowser控件内部执行C#应用程序代码?
- 28. 是否可以从基于Web的应用程序执行Objective C代码
- 29. C#Windows窗体应用程序关闭
- 30. 关闭C#窗体应用程序
你到目前为止尝试了什么?请发布您的代码 – demonplus
我试图使用IronPython和ProcessStartInfo,但IronPython不工作,每次我尝试和ProcessStartInfo会自动关闭。这里是我的项目https://www.mediafire.com/?81ppkgl5fpel595 –