C# 执行CMD命令
/// <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(); }