using System;
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Text;
class EdtUtil
{
///
/// 构建Process对象,并执行
///
/// 命令
/// 命令的参数
/// 工作目录
/// Process对象
private static System.Diagnostics.Process CreateCmdProcess(string cmd, string args, string workingDir = "")
{
var en = System.Text.UTF8Encoding.UTF8;
if (Application.platform == RuntimePlatform.WindowsEditor)
en = System.Text.Encoding.GetEncoding("gb2312");
var pStartInfo = new System.Diagnostics.ProcessStartInfo(cmd);
pStartInfo.Arguments = args;
pStartInfo.CreateNoWindow = false;
pStartInfo.UseShellExecute = false;
pStartInfo.RedirectStandardError = true;
pStartInfo.RedirectStandardInput = true;
pStartInfo.RedirectStandardOutput = true;
pStartInfo.StandardErrorEncoding = en;
pStartInfo.StandardOutputEncoding = en;
if (!string.IsNullOrEmpty(workingDir))
pStartInfo.WorkingDirectory = workingDir;
return System.Diagnostics.Process.Start(pStartInfo);
}
///
/// 运行命令,不返回stderr版本
///
/// 命令
/// 命令的参数
/// 工作目录
/// 命令的stdout输出
public static string RunCmdNoErr(string cmd, string args, string workingDri = "")
{
var p = CreateCmdProcess(cmd, args, workingDri);
var res = p.StandardOutput.ReadToEnd();
p.Close();
return res;
}
///
/// 运行命令,不返回stderr版本
///
/// 命令
/// 命令的参数
/// StandardInput
/// 工作目录
/// 命令的stdout输出
public static string RunCmdNoErr(string cmd, string args, string[] input, string workingDri = "")
{
var p = CreateCmdProcess(cmd, args, workingDri);
if (input != null && input.Length > 0)
{
for (int i = 0; i < input.Length; i++)
p.StandardInput.WriteLine(input[i]);
}
var res = p.StandardOutput.ReadToEnd();
p.Close();
return res;
}
///
/// 运行命令
///
/// 命令
/// 命令的参数
/// string[] res[0]命令的stdout输出, res[1]命令的stderr输出
public static string[] RunCmd(string cmd, string args, string workingDir = "")
{
string[] res = new string[2];
var p = CreateCmdProcess(cmd, args, workingDir);
res[0] = p.StandardOutput.ReadToEnd();
res[1] = p.StandardError.ReadToEnd();
p.Close();
return res;
}
///
/// 打开文件夹
///
/// 文件夹的绝对路径
public static void OpenFolderInExplorer(string absPath)
{
if (Application.platform == RuntimePlatform.WindowsEditor)
RunCmdNoErr("explorer.exe", absPath);
else if (Application.platform == RuntimePlatform.OSXEditor)
RunCmdNoErr("open", absPath.Replace("\\", "/"));
}
文章来源地址https://www.toymoban.com/news/detail-761986.html
文章来源:https://www.toymoban.com/news/detail-761986.html
到了这里,关于Unity中-C#执行Cmd命令(System.Diagnostics.Process的使用)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!