C# 使用cmd

public static string cmd(String command)               //向cmd()传入命令行,传入"exit"则退出cmd.exe。
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;                  //这里是关键点,不用Shell启动
            p.StartInfo.RedirectStandardInput = true;             //重定向输入
            p.StartInfo.RedirectStandardOutput = true;            //重定向输出
            p.StartInfo.CreateNoWindow = true;                    //不显示窗口
            p.Start();
            p.StandardInput.WriteLine(command);// 向cmd.exe输入command
            //p.WaitForExit();
            p.StandardInput.WriteLine("exit");
            string s = p.StandardOutput.ReadToEnd();// 得到cmd.exe的输出
            p.Close();
            return s;
        }

大佬的代码。
原文地址:https://www.cnblogs.com/miaololi/p/9122344.html