2015-05-14 57 views
0

作为一个学校项目,我的课程在python 3中制作了一个简单的编程语言。现在我们在c#中做了一个简单的ide,它应该在新的控制台窗口中执行python脚本。我想知道什么是最有效的方法。 (我应该用参数来执行)从c#窗体应用程序执行python 3代码

+0

你到目前为止尝试了什么?请发布您的代码 – demonplus

+0

我试图使用IronPython和ProcessStartInfo,但IronPython不工作,每次我尝试和ProcessStartInfo会自动关闭。这里是我的项目https://www.mediafire.com/?81ppkgl5fpel595 –

回答

1

您可以使用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(); 
+0

我试过这个和consloe打开和关闭没有任何输出 –

+0

让我调查 – Jaco

+0

我在这里上传了项目https://www.mediafire.com/?81ppkgl5fpel595 –

0

有运行python脚本双向:

  1. 一种方法是运行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"); 
    } 
    
    相关问题