牛骨文教育服务平台(让学习变的简单)
博文笔记

C# 执行CMD返回信息

创建时间:2015-05-09 投稿人: 浏览次数:114


using System.Diagnostics;


  /// <summary>
        /// 执行CMD命令返回信息
        /// </summary>
        /// <param name="Command">命令</param>
        /// <returns>返回命令执行结果</returns>
        public string executeCmd(string Command)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe ";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine(Command);
            p.StandardInput.WriteLine("exit");
            System.Threading.Thread.Sleep(1000);
         string Re=   p.StandardOutput.ReadToEnd();
         p.StandardOutput.Close();
         p.WaitForExit();
         p.Close();
            return Re;
        }


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。