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:\Users\admin>" + 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 + "\n";

            while (!reader.EndOfStream)
            {
                line = reader.ReadLine();
                text1.text += line + "\n";
            }

            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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM