C#调用CMD输命令接收返回值
例如:
string c = @"net start ServiceDemo";
Cmd(c);
Cmd(c);
string c = @"c:\windows\microsoft.net\framework\v2.0.50727\InstallUtil.exe " + AppDomain.CurrentDomain.BaseDirectory + "ServiceDemo.exe";
Cmd(c);
/// <summary> /// 执行Cmd命令 /// </summary> 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(); }