码迷,mamicode.com
首页 > Windows程序 > 详细

C# 执行CMD命令

时间:2016-05-24 14:58:59      阅读:208      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!