美文网首页Linux学习|Gentoo/Arch/FreeBSD
Unity调用python等其它程序 2022-10-14

Unity调用python等其它程序 2022-10-14

作者: 午字横 | 来源:发表于2023-03-30 23:02 被阅读0次
    1:C#/.NET 中启动进程时所使用的 UseShellExecute 设置为 true 和 false 分别代表什么意思?

    https://blog.csdn.net/WPwalter/article/details/90344443

    **本质差异**
    Process.Start 本质上是启动一个新的子进程,不过这个属性的不同,使得启动进程的时候会调用不同的 Windows 的函数。
    
    UseShellExecute = true
    调用的是 ShellExecute
    UseShellExecute = false
    调用的是 CreateProcess
    当然,如果你知道这两个函数的区别,那你自然也就了解此属性设置为 true 和 false 的区别了。
    
    效果差异
    ShellExecute 的用途是打开程序或者文件或者其他任何能够打开的东西(如网址)。
    
    也就是说,你可以在 Process.Start 的时候传入这些:
    
    一个可执行程序(exe)
    一个网址
    一个 html / mp4 / jpg / docx / enbx 等各种文件
    在 PATH 环境变量中的各种程序
    不过,此方法有一些值得注意的地方:
    
    不支持重定向输入和输出
    最终启动了哪个进程可能是不确定的,你可能需要注意潜在的安全风险
    而 CreateProcess 则会精确查找路径来执行,不支持各种非可执行程序的打开。但是:
    
    支持重定向输入和输出
    如何选择
    UseShellExecute 的默认值是 true。
    
    如果有以下需求,那么建议设置此值为 false:
    
    需要明确执行一个已知的程序
    需要重定向输入和输出
    如果你有以下需求,那么建议设置此值为 true 或者保持默认:
    
    需要打开文档、媒体、网页文件等
    需要打开 Url
    需要打开脚本执行
    需要打开计算机上环境变量中路径中的程序
    ————————————————
    版权声明:本文为CSDN博主「walter lv」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/WPwalter/article/details/90344443
    
    using System.Diagnostics;
    public class py : MonoBehaviour
    {
        void Start()
        {
            RunPythonScirpts();
        }
        void RunPythonScirpts()
        {
            Process p = new Process();
            //python代码路径
            //string path = @"D:\PythonSpace\TensorFlow2.X\unityTest\01.py";
            string path = @"https://blog.csdn.net/WPwalter/article/details/90344443";
            //https://blog.csdn.net/WPwalter/article/details/90344443
            //python代码解释器
            //p.StartInfo.FileName = @"C:\Users\liu\.conda\envs\python_3.6_tf_2.0_demo\python.exe";
            p.StartInfo.FileName = @"C:\Program Files\Google\Chrome\Application\chrome.exe";
    
            //在python种实用sys.argv[] 使用该参数
            p.StartInfo.UseShellExecute = false;
    
            p.StartInfo.Arguments = path;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardInput = true;
    
            //p.StartInfo.CreateNoWindow = true;
    
    
            p.Start();
            p.BeginOutputReadLine();
            p.OutputDataReceived += new DataReceivedEventHandler(get_data);
        }
    
        public  void get_data(object sender,DataReceivedEventArgs e)
        {
            print(e.Data);
        }
    }
    
    

    相关文章

      网友评论

        本文标题:Unity调用python等其它程序 2022-10-14

        本文链接:https://www.haomeiwen.com/subject/iioqzrtx.html