CmdHelper

using System.Diagnostics;
using System.IO;

namespace GT.Common.Helper
{
    /// <summary>
    /// Dos cmd命令执行帮助类
    /// </summary>
    public class CmdHelper
    {
        /// <summary>
        /// 运行dos命令
        /// </summary>
        /// <Param name="command"></Param>
        /// <returns></returns>
        public static string Run(string command)
        {
            string str = "";
            ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c " + command)
            {
                RedirectStandardOutput = true,
                UseShellExecute = false,
                RedirectStandardError = true,
                CreateNoWindow = true
            };
            using (Process process = Process.Start(startInfo))
            {
                using (StreamReader reader = process.StandardOutput)
                {
                    str = reader.ReadToEnd();
                }
                process.WaitForExit();
            }
            return str.Trim();
        }
    }
}

 

posted @ 2019-09-02 14:20  萌橙  阅读(424)  评论(0编辑  收藏  举报