unity3d:process 进程调用C#控制台程序,并获得输出。传入参数有空格要加““

2023-08-24 14:55:46 浏览数 (2)

代码语言:javascript复制
public static Process StartProcess(string fileName,string args)
    {
        try
        {
            fileName = """   fileName   """;
            //args = """   args   """;
            Process myProcess = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo(fileName, args);
            startInfo.CreateNoWindow = true;
            startInfo.RedirectStandardInput = true;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            myProcess.StartInfo = startInfo;
            return myProcess;
        }
        catch (Exception ex)
        {
            UnityEngine.Debug.Log("出错原因:"   ex.Message);
        }
        return null;
}

使用示例:

代码语言:javascript复制
string param = """   "立羽"   """;
        Process pro = PublicFunc.StartProcess(Application.streamingAssetsPath   "/PngAddText/TextToPng.exe",param);
        pro.Start();

        string fingerprint = pro.StandardOutput.ReadLine();
        UnityEngine.Debug.Log(fingerprint);
        pro.WaitForExit();
        pro.Close();

0 人点赞