是否可以从 C# Form App 运行 Anaconda 脚本?

时间:2021-05-16 16:53:21

标签: python c#

我正在创建 C# 表单应用程序,我想创建一个按钮,该按钮将帮助我在单击时运行 anaconda 脚本。

我想要的脚本:“python AllInOne.py”

我可以在 C# 表单按钮上运行这个脚本吗?我怎样才能实现它?

1 个答案:

答案 0 :(得分:1)

private void run_cmd(string cmd, string args)
{
     ProcessStartInfo start = new 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);
         }
     }
}

using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

private static void doPython()
{
    ScriptEngine engine = Python.CreateEngine();
    engine.ExecuteFile(@"AllInOne.py");
}