angel725
V2EX  ›  问与答

c#调用 python3 程序

  •  
  •   angel725 · Sep 25, 2018 · 2423 views
    This topic created in 2817 days ago, the information mentioned may be changed or developed.
    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 replies    2018-09-26 18:09:37 +08:00
    ysc3839
        1
    ysc3839  
       Sep 25, 2018
    RedirectStandardOutput 的问题?
    xujinkai
        2
    xujinkai  
       Sep 25, 2018
    你设置了 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
       Sep 26, 2018
    wel.py 单独运行正常。但点了按钮,什么反应都没有

    wel.py 里面作用是打开浏览器。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2781 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 11:31 · PVG 19:31 · LAX 04:31 · JFK 07:31
    ♥ Do have faith in what you're doing.