Calmer的文章

  • 首页
  • 文章归档
  • 关于页面

  • 搜索
体验游戏 笔记 推荐 工具链 工具使用 小游戏 插件 UI 软件 教程

CommandTool

发表于 2020-06-21 | 分类于 游戏开发 | 0 | 阅读次数 741

前言

有时候需要用代码执行一些命令行命令


C#代码

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

public class CommandTool
{
    public static ExecuteCommandResult Excute(string fileName, string arguments)
    {
        List<string> msg = new List<string>();
        Process p = new Process();
        p.StartInfo.FileName = fileName;
        p.StartInfo.Arguments = arguments;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.StandardOutputEncoding = Encoding.GetEncoding("gbk");
        p.StartInfo.StandardErrorEncoding = Encoding.GetEncoding("gbk");
        p.Start();
        string resultStr = p.StandardOutput.ReadToEnd();
        string errorStr = p.StandardError.ReadToEnd();
        p.WaitForExit();
        p.Close();
        foreach (string str in resultStr.Split(new char[] { '\r' }))
        {
            msg.Add(str.Trim());
        }
        bool sucess = true;
        if (errorStr != "")
        {
            sucess = false;
            foreach (string str in errorStr.Split(new char[] { '\r' }))
            {
                msg.Add(str.Trim());
            }
        }
        return new ExecuteCommandResult(sucess, msg);
    }

    public class ExecuteCommandResult
    {
        public bool sucess;
        public List<string> msg;
        public ExecuteCommandResult(bool sucess, List<string> msg)
        {
            this.sucess = sucess;
            this.msg = msg;
        }
        override public string ToString()
        {
            string result = "";
            msg.ForEach(v => result += v);
            return result;
        }
    }
}

Python代码

无

  • 本文作者: Calmer
  • 本文链接: https://mytechplayer.com/archives/record02
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
# 工具链
Unity目录结构
FairyGUI的学习
  • 文章目录
  • 站点概览
Calmer

Calmer

88 日志
7 分类
10 标签
RSS
Creative Commons
0%
© 2020 — 2025 Calmer
由 Halo 强力驱动
蜀ICP备20010026号-1川公网安备51019002006543
Copyright © 2020-2025 Calmer的文章