zoukankan      html  css  js  c++  java
  • Unity调起外部程序cmd.exe等

      C#中的Process类可以访问windows所有的进程,当然也可以调用Windows命令行了,具体用法参见官方文档:

      https://docs.microsoft.com/zh-cn/dotnet/api/system.diagnostics.process?redirectedfrom=MSDN&view=netframework-4.8

      用途:执行外部的批处理命令
      例如:
      a.在U3D的编辑器类中操作CMD脚本,自动生成Protobuf文件。
      b.资源的自动导入导出
      c.调用外部程序显示,打开外部程序
     
      
    using System.Diagnostics;  
    
    public static string RunCMD(string command)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe"//确定程序名
            p.StartInfo.Arguments = @"C:Usersadmin>" + command;  //指定程式命令行
            p.StartInfo.UseShellExecute = false;   //是否使用Shell
            p.StartInfo.RedirectStandardInput = true;   //重定向输入
            p.StartInfo.RedirectStandardOutput = true;   //重定向输出
            p.StartInfo.RedirectStandardError = true;    //重定向输出错误
            p.StartInfo.CreateNoWindow = false;        //设置不显示窗口
            p.Start();
            return p.StandardOutput.ReadToEnd();     //输出流取得命令行结果
        }

      在Unity主线程调用会被锁死(阻塞),通过新开线程解决

      

    public void Test1()
        {
            Thread t = new Thread(new ThreadStart(NewThread));
            t.Start();
        }
    
        public void NewThread()
        {
            string cmd = "cd f";
            string str = RunCmd_A(cmd);
            UnityEngine.Debug.Log(str);
        }
    
       
        public string RunCmd_A(string command)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = command;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = false;
            p.Start();
            text1.text += p.StandardOutput.ReadToEnd();
    
            return p.StandardOutput.ReadToEnd();
        }

    另外一种读取StandardOutput的方式

    public void RunCmd_B()
        {
    
            string cmd = "ping www.baidu.com";
            cmd = cmd.Trim().TrimEnd('&');// + "&exit";
                
            using (Process p = new Process())
            {
                
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = false;
                p.Start();
    
                
                p.StandardInput.WriteLine(cmd);
                p.StandardInput.AutoFlush = true;
    
                p.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
                p.StartInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;
    
                print(p.StandardOutput.CurrentEncoding);
            
                
                StreamReader reader = p.StandardOutput;
                
                
                string line = reader.ReadLine();
    
                
                text1.text += line + "
    ";
    
                while (!reader.EndOfStream)
                {
                    line = reader.ReadLine();
                    text1.text += line + "
    ";
                }
    
                p.WaitForExit();
                p.Close();
                
            }
        }

    这里编码格式是Utf-8,但是windows cmd窗口的默认格式是GBK所以这里会乱码,解决方案

    1、打开CMD.exe命令行窗口 

    2、通过 chcp命令改变代码页,UTF-8的代码页为65001 

    chcp 65001 

    执行该操作后,代码页就被变成UTF-8了。但是,在窗口中仍旧不能正确显示UTF-8字符。 

    3、修改窗口属性,改变字体 

    在命令行标题栏上点击右键,选择"属性"->"字体",将字体修改为True Type字体"Lucida Console",然后点击确定将属性应用到当前窗口。 

    CMD更换默认编码参考

    https://blog.csdn.net/iway_lch/article/details/50408796

    另一种转换编码格式输出思想    https://www.cnblogs.com/zjxyz2008zhangjuan/p/7246646.html , 不过自己测试似乎并不管用

    编码转换参考   https://www.cnblogs.com/zhshlimi/p/5605512.html

    调用CMD窗口,但是不重定向输出,不关闭CMD窗口,将输出显示在CMD窗口 , 注意 : Arguments这种传命令的方法不加“/k“的话,命令不会执行,“/k”是cmd的命令,使用StandardInput.WriteLine传命令的话不需要加“/k”,但是关闭重定向输入后,input使用不了

    process.StartInfo.UseShellExecute = true;
    process.StartInfo.RedirectStandardInput = false;
    process.StartInfo.RedirectStandardOutput = false;
    然后以下面的方式传参数
    process.StartInfo.Arguments = "/k command" 单个命令
    或
    process.StartInfo.Arguments = "/k command.bat" bat中可以写多个命令
    启动
    process.start();
    
    command命令中加&Pause

     4.bat文件调用.exe程序传入参数

    https://zhidao.baidu.com/question/75526803.html

    https://blog.csdn.net/weixin_38601426/article/details/84887275

    https://www.cnblogs.com/qiu18359243869/p/10952240.html

  • 相关阅读:
    c#对XML读取
    WPF--TypeConverter使用
    WPF---对于没有Command属性的添加以下代码可以达到有Command效果
    自定义事件、属性、方法
    读取Excel文件
    ClickOnce安装部署,手动。
    Logger 日志记录
    Maven
    等待与通知范式
    线程状态及基本方法
  • 原文地址:https://www.cnblogs.com/white-L/p/11089617.html
Copyright © 2011-2022 走看看