C#执行外部命令的方法
本文实例讲述了C#执行外部命令的方法。分享给大家供大家参考。具体实现方法如下:
///<summary>
///executesasystemcommandfrominsidecsharp
///</summary>
///<paramname="cmd">adostypecommandlike"isql..."</param>
///<paramname="millsecTimeOut">howlongtowaitonthecommand</param>
publicstaticintexecuteCommand(stringcmd,intmillsecTimeOut){
System.Diagnostics.ProcessStartInfoprocessStartInfo=newSystem.Diagnostics.ProcessStartInfo("CMD.exe","/C"+cmd);
processStartInfo.CreateNoWindow=true;
processStartInfo.UseShellExecute=false;
System.Diagnostics.Processprocess=System.Diagnostics.Process.Start(processStartInfo);
process.WaitForExit(millsecTimeOut);//waitfor20sec
intexitCode=process.ExitCode;
process.Close();
returnexitCode;
}
希望本文所述对大家的C#程序设计有所帮助。