首页 > Windows开发 > 详细

C# 执行CMD命令

时间:2016-05-24 14:57:50      阅读:177      评论:0      收藏:0      [点我收藏+]
        /// <summary>
        /// 执行Cmd命令
        /// </summary>
        public void ExecCmd(string cmdstr)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.Start();

            process.StandardInput.WriteLine(cmdstr);
            process.StandardInput.AutoFlush = true;
            process.StandardInput.WriteLine("exit");

            StreamReader reader = process.StandardOutput;//截取输出流

            string output = reader.ReadLine();//每次读取一行

            while (!reader.EndOfStream)
            {
                PrintThrendInfo(output);
                output = reader.ReadLine();
            }

            process.WaitForExit();
        }

 

C# 执行CMD命令

原文:http://www.cnblogs.com/ilookbo/p/5523115.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!