入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

C# 执行CMD返回信息

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


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;
        }


声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。