V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
angel725
V2EX  ›  问与答

c#调用 python3 程序

  •  
  •   angel725 · 2018-09-25 11:02:21 +08:00 · 1563 次点击
    这是一个创建于 2042 天前的主题,其中的信息可能已经有所发展或是发生改变。
    private void button3_Click(object sender, EventArgs e)
    {
    Process p = new Process();
    string path = @"H:\src\wel.py";
    p.StartInfo.FileName = @"E:\Python\Python36\python.exe";
    p.StartInfo.Arguments = path;
    Console.WriteLine(path);
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.CreateNoWindow = true;
        p.Start();
    初接触 c#,因为它方便,所以想用它做 GUI,调用 python 程序。

    百度学习了下,有个用进程启动 python 的方法。

    但为什么我的不能用呢?

    点了按钮,什么反应都没有
    3 条回复    2018-09-26 18:09:37 +08:00
    ysc3839
        1
    ysc3839  
       2018-09-25 13:42:02 +08:00
    RedirectStandardOutput 的问题?
    xujinkai
        2
    xujinkai  
       2018-09-25 15:21:34 +08:00
    你设置了 UseShellExecute 和 CreateNoWindow 当然是没反应的,当然是看不到窗口的
    RedirectStandardOutput 以后,需要使用 p.OutputDataReceived += ... 来获取输出

    MSDN https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.outputdatareceived?redirectedfrom=MSDN&view=netframework-4.7.2
    这是我写的 https://xujinkai.net/posts/csharp-use-process-run-command
    angel725
        3
    angel725  
    OP
       2018-09-26 18:09:37 +08:00
    wel.py 单独运行正常。但点了按钮,什么反应都没有

    wel.py 里面作用是打开浏览器。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3130 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:53 · PVG 20:53 · LAX 05:53 · JFK 08:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.