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#/.NET 中启动进程时所使用的 UseShellExecute 设置为 true 和 false 分别代表什么意思?_walter lv的博客-CSDN博客_useshellexecute
ImageMagick 的安装及使用 - Rogn - 博客园 (cnblogs.com)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)