C# cmd执行命令

时间:2022-10-19 16:56:10
CMD命令执行
///<summary>
        /// cmd命令执行,在cmd上可以执行的语句,直接传到这里,调用grads画图实例如下:
        ///  Cmd("C:/OpenGrADS/Contents/Cygwin/Versions/2.0.1.oga.1/i686/grads.exe -lbcx 'D:/data_wrfchem/gs/d01_hour_pm25.gs D:/data_wrfchem 2016-04-18 d01'");
        ///  Cmd("grads启动程序exe路径 -lbcx 'gs脚本文件 参数1 参数2 参数3'");
        /// </summary>
        /// <param name="c">执行语句</param>
        public void Cmd(string c)
        {
            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(c);
            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();
        }