private static string CmdPath = @"C:\Windows\System32\cmd.exe";
public static void RunCmd(string cmd, out string output)
{
cmd = cmd.Trim().TrimEnd('&') + "&exit";//说明:不管命令是否成功均执行exit命令,否则当调用ReadToEnd()方法时,会处于假死状态
UnityEngine.Debug.Log(cmd);
using (Process p = new Process())
{
p.StartInfo.FileName = CmdPath;
p.StartInfo.Arguments = "/c " + cmd;
p.StartInfo.UseShellExecute = false; //不使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true; //接受来自Unity的输入信息
p.StartInfo.RedirectStandardOutput = true; //从Unity控制台获取输出信息
p.StartInfo.RedirectStandardError = true; //从Unity控制台定向标准错误输出
p.StartInfo.CreateNoWindow = true; //不显示程序窗口,默默执行
p.Start();//启动程序
//获取cmd窗口的输出信息
output = p.StandardOutput.ReadToEnd();
p.WaitForExit();//等待程序执行完退出进程
p.Close();
}
}
Unity中如何默默的执行cmd批处理命令
最新推荐文章于 2024-07-22 15:33:01 发布