作为一个学校项目,我的课程在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. C#代码编辑器控制窗体应用程序
- 8. 从Windows窗体应用程序C控制台应用程序#
- 9. Android应用程序中的C/C++代码如何执行?
- 10. C#窗体窗体应用程序,从子窗体关闭父窗体
- 11. 如何从perl程序执行python代码并使用python代码的输出
- 12. 从Windows执行批处理文件窗体应用程序
- 13. Cython:从C程序调用Python代码
- 14. 从Windows窗体应用程序退出代码
- 15. 错误从C#窗体窗体应用程序连接到SQL
- 16. 从Datatable C#Windows窗体应用程序中选择前N行
- 17. 如何从C#窗体表单应用程序执行.bat文件?
- 18. 在执行主窗口窗体应用程序类之前执行其他类
- 19. C#从服务(和Vista中)运行Windows窗体应用程序
- 20. 调度程序不在C#窗体窗体应用程序
- 21. 从C#执行excel应用程序
- 22. c#窗体窗体应用程序窗体问题
- 23. Windows窗体代码没有执行?
- 24. 窗体中的代码执行停止
- 25. 如何在WinRT C#应用程序中执行Javascript代码?
- 26. C#聊天应用程序执行代码时会冻结
- 27. 用代码01退出的C++程序没有执行代码
- 28. 如何轻松在C#窗体窗体应用程序中密码密码?
- 29. RegistryKey在Windows窗体应用程序上执行的操作
- 30. 窗体窗体应用程序“查找”
你到目前为止尝试了什么?请发布您的代码 – demonplus
我试图使用IronPython和ProcessStartInfo,但IronPython不工作,每次我尝试和ProcessStartInfo会自动关闭。这里是我的项目https://www.mediafire.com/?81ppkgl5fpel595 –