可以执行多条命令,用“/r/n”分割
1 using System; 2 using System.Diagnostics; 3 4 namespace Tool 5 { 6 7 public class CMDHelper 8 { 9 public static string[] ExeCommand(string commandText)10 {11 12 PRocess p = new Process();13 p.StartInfo.FileName = Environment.GetEnvironmentVariable("ComSpec");14 p.StartInfo.UseShellExecute = false;15 p.StartInfo.RedirectStandardInput = true;16 p.StartInfo.RedirectStandardOutput = true;17 p.StartInfo.RedirectStandardError = true;18 p.StartInfo.CreateNoWindow = true;19 20 p.Start();21 p.StandardInput.WriteLine(commandText);22 p.StandardInput.WriteLine("exit");23 p.WaitForExit();24 25 string strOutput = p.StandardOutput.ReadToEnd();26 string strError = p.StandardError.ReadToEnd();//无错误则返回空字符串27 p.Close();28 return new string[] { strOutput, strError };29 }30 }31 }
新闻热点
疑难解答