Unity | call external exe

1. Call another Unity EXE software

    /// <summary>
    /// 打开外部程序
    /// </summary>
    /// <param name="_exePathName">EXE所在绝对路径及名称,带后缀.exe</param>
    /// <param name="_exeArgus">启动参数</param>
    public static void StartEXE(string _exePathName, string _exeArgus)
    {
        try
        {
            System.Diagnostics.Process myprocess = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(@_exePathName, _exeArgus);
            myprocess.StartInfo = startInfo;
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.Start();
        }
        catch (Exception ex)
        {
           Debug.Log("加载exe失败," + ex.Message);
        }
    }

Second, call the console application

Loading the console application needs to set the WorkingDirectory property (guess the reason: the console application loaded in my project needs to call other dlls when it starts. This dll needs to load the local model file, so the current working directory of the exe needs to be set), otherwise it will Failed to load.

    /// <summary>
    /// 打开外部程序
    /// </summary>
    /// <param name="_exePathName">EXE所在绝对路径,不带名称</param>
    /// <param name="_exeName">exe名称,带后缀</param>
    public static void StartEXE(string _exePathName,string _exeName)
    {
        try
        {
            System.Diagnostics.Process myprocess = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(@_exePathName+ "/"+_exeName);
            myprocess.StartInfo = startInfo;
            myprocess.StartInfo.WorkingDirectory = @_exePathName;
            myprocess.StartInfo.CreateNoWindow = true;
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.StartInfo.RedirectStandardOutput = true;
            myprocess.Start();
        }
        catch (Exception ex)
        {
            Debug.Log("加载exe失败," + ex.Message);
        }
    }

3. Close the process

    /// <summary>
    /// 杀死进程
    /// </summary>
    /// <param name="_exePathName">进程名称,不带路径及后缀</param>
    public static void CloseExe(string _exePathName)
    {
        System.Diagnostics.Process[] progress = System.Diagnostics.Process.GetProcessesByName(_exePathName);
        Debug.Log("progress个数:" + progress.Length);
        for (int i = 0; i < progress.Length; i++)
        {
            progress[i].Kill();
        }
    }

4. Precautions for path loading

If the path of the third-party exe is configured through config.txt, then you need to pay attention when reading this path string:

  1. method one

The path string is on a line by itself . Then when reading a string, pay attention that its length is 1 bit more than the actual string, so string processing is required.

  • Configuration file :

C:\Users\fengchujun\Desktop\zhanhuiexe\pipiGame\PipiDriver.exe
  • Read path string :

  IEnumerator GetConfigData()
    {
        using (UnityWebRequest w = UnityWebRequest.Get(Application.streamingAssetsPath + "/configV2.1.0.txt"))
        {
            yield return w.SendWebRequest();
            if (w.isNetworkError || w.isHttpError)
            {
                Debug.Log("GetConfig error");
            }
            else
            {
                string data = w.downloadHandler.text;
                string[] _array = data.Split('\n', System.StringSplitOptions.RemoveEmptyEntries);
              
                ExePath=_array[0];
            }
        }
    }
  • String processing and calling

 if (ExePath != "")
 {
      string  path = @ExePath.Substring(0,ExePath.Length-1);
      Debug.Log(path.Length+":"+ path);
      StartEXE(path, null);
 }
  1. way two

If the path is separated by spaces and other data in one line , then this path string does not need to be processed.

  • Configuration file :

(Note that there is a space after the exe)

1080 720 25 2 5 14 17 C:\Users\fengchujun\Desktop\zhanhuiexe\pipiGame\PipiDriver.exe 
  • Read path string :

 IEnumerator GetConfig()
  {
        using (UnityWebRequest w = UnityWebRequest.Get(Application.streamingAssetsPath + "/config.txt"))
        {
            yield return w.SendWebRequest();
            if (w.isNetworkError || w.isHttpError)
            {
                Debug.Log("GetConfig error");
            }
            else
            {
                string info = w.downloadHandler.text;
                string[] _array = info.Split('\n', System.StringSplitOptions.RemoveEmptyEntries);
                Debug.Log(_array.Length);

                //get cameraInfo
                string[] cameraInfo = _array[0].Split(' ', System.StringSplitOptions.RemoveEmptyEntries);
                Debug.Log(cameraInfo.Length);
                //...
                softExePath= cameraInfo[7];
            }
       } 
}
  • transfer

    /// <summary>
    /// 开启第三方exe
    /// </summary>
    /// <returns></returns>
    void StartExe()
    {
        CommonData.WriteIntoTxt("第三方软件exe:" + softExePath);
        if (softExePath != "")
        {
            CommonData.StartEXE(softExePath, null);
        }
    }

Guess you like

Origin blog.csdn.net/weixin_39766005/article/details/129324489