当前位置:网站首页>[unity] use C in unity to execute external files, such as Exe or bat

[unity] use C in unity to execute external files, such as Exe or bat

2022-06-26 17:59:00 T.D.C

Sample code

            var process = ExecuteFile(workPath, exePath);
            if (process.Start())
            {
    
                Debug.Log(ReadToEnd(process.StandardOutput));
            }

            Debug.LogError(ReadToEnd(process.StandardError));

Tool code

		public static string ReadToEnd(StreamReader reader)
        {
            var sbd = new StringBuilder();
            while (true)
            {
                var readLine = reader.ReadLine();
                if (readLine == null)
                {
                    break;
                }

                sbd.AppendLine(readLine);
            }
            return sbd.ToString();
        }

        public static Process ExecuteFile(string workDir, string executeFile)
        {
            var process = new Process();
            var startInfo = new ProcessStartInfo(executeFile)
            {
                WorkingDirectory = workDir,
                CreateNoWindow = false,
                UseShellExecute = false,
                WindowStyle = ProcessWindowStyle.Hidden,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                StandardOutputEncoding = Encoding.GetEncoding("GB2312"),
                StandardErrorEncoding = Encoding.GetEncoding("GB2312"),
            };
            process.StartInfo = startInfo;
            return process;
        }
原网站

版权声明
本文为[T.D.C]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261710244472.html