首页 > 学院 > 开发设计 > 正文

执行CMD命令

2019-11-17 03:15:47
字体:
来源:转载
供稿:网友

执行CMD命令

可以执行多条命令,用“/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 }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表