unity下执行命令行

代码

#if UNITY_EDITOR

using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

public class CmdTool
{
    private static Queue<string> _cmdQueue = new Queue<string>();

    [MenuItem("MyTools/ShowJavaVersion", false)]
    static void ShowJavaVersion()
    {
        AddCmd("java -version");
        RunCmd();
    }

    public static void AddCmd(string cmd)
    {
        _cmdQueue.Enqueue(cmd);
    }

    public static void RunCmd()
    {
        if (_cmdQueue.Count <= 0)
        {
            Debug.Log("finish");
            return;
        }

        using (var process = new System.Diagnostics.Process())
        {
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.WorkingDirectory = @"C:\"; //不设置就是当前目录
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            process.StartInfo.CreateNoWindow = true; //不显示命令行的黑窗口
            process.Start(); //启动程序

            var cmd = _cmdQueue.Dequeue();
            //process.StandardInput.AutoFlush = true; //下面手动Flush了, 所以这边可以注释掉
            process.StandardInput.WriteLine(cmd); //执行命令
            process.StandardInput.WriteLine("exit"); //执行完后自动退出cmd
            process.StandardInput.Flush(); //或 process.StandardInput.Close(); 

            process.WaitForExit(); //等待程序执行完退出进程
            if (process.HasExited)
            {
                PrintAllLines(process.StandardOutput);
                PrintAllLines(process.StandardError);

                RunCmd();
            }
        }
    }

    private static void PrintAllLines(StreamReader sr)
    {
        var gbkEnc = Encoding.GetEncoding("gb2312"); //windows的命令行默认是gbk字符集的
        using (StreamReader sr2 = new StreamReader(sr.BaseStream, gbkEnc)) //换成gbk的Reader来读
        {
            while (!sr2.EndOfStream)
            {
                string curLine = sr2.ReadLine();
                if (!string.IsNullOrEmpty(curLine))
                {
                    Debug.Log($"{curLine}");
                }
            }
        }
    }

}

#endif

执行ShowJavaVersion的运行结果:

 

其他例子 

1) 记事本打开c#代码文件

var process = new System.Diagnostics.Process();
process.StartInfo.FileName = "notepad.exe";
process.StartInfo.Arguments = "C:\\Users\\win\\Documents\\Test.cs";
process.Start();

 

2) 用资源管理器打开c#代码文件

System.Diagnostics.Process.Start("explorer.exe", "C:\\Users\\win\\Documents\\Test.cs");

运行后,出现了程序选择文件

 

 


3) 打开资源管理器

var processStartInfo = new System.Diagnostics.ProcessStartInfo();
processStartInfo.FileName = "explorer.exe"; //资源管理器
processStartInfo.Arguments = @"c:\";
System.Diagnostics.Process.Start(processStartInfo);


4) 用浏览器打开百度

var process = new System.Diagnostics.Process();
process.StartInfo.FileName = "360se.exe";
process.StartInfo.Arguments = "http://www.baidu.com";
process.Start();

这边没装360,报错了


5) 使用ImageMagick处理图片

var srcFile = "C:/Users/win/Documents/src.png";
var resultFile = "C:/Users/win/Documents/src-resize.png";
var cmd = $"magick convert -resize 300x600! {srcFile} {resultFile}";
CmdTool.AddCmd(cmd);
CmdTool.RunCmd();

处理后的图片

 

参考

C#:为什么我的Process运行Java.exe工作正常,但窗口没有返回任何输出? - VoidCC

用户对问题“如何在C#中逐行读取命令输出结果”的回答 - 问答 - 腾讯云开发者社区-腾讯云 (tencent.com)

c#process的详细用法_新创美的博客-CSDN博客

C#/.NET 中启动进程时所使用的 UseShellExecute 设置为 true 和 false 分别代表什么意思?_walter lv的博客-CSDN博客_useshellexecute

ImageMagick 的安装及使用 - Rogn - 博客园 (cnblogs.com) 

 

posted @ 2022-11-20 13:33  yanghui01  阅读(697)  评论(0编辑  收藏  举报