/// <summary>
/// 運行cmd命令
/// 會顯示命令窗口
/// </summary>
/// <param name="cmdExe">指定應(yīng)用程序的完整路徑</param>
/// <param name="cmdStr">執(zhí)行命令行參數(shù)</param>
static bool RunCmd(string cmdExe, string cmdStr)
{
bool result = false;
try
{
using (Process myPro = new Process())
{
//指定啟動進程是調(diào)用的應(yīng)用程序和命令行參數(shù)
ProcessStartInfo psi = new ProcessStartInfo(cmdExe, cmdStr);
myPro.StartInfo = psi;
myPro.Start();
myPro.WaitForExit();
result = true;
}
}
catch
{
}
return result;
}
/// <summary>
/// 運行cmd命令
/// 不顯示命令窗口
/// </summary>
/// <param name="cmdExe">指定應(yīng)用程序的完整路徑</param>
/// <param name="cmdStr">執(zhí)行命令行參數(shù)</param>
static bool RunCmd2(string cmdExe, string cmdStr)
{
bool result = false;
try
{
using (Process myPro = new Process())
{
myPro.StartInfo.FileName = "cmd.exe";
myPro.StartInfo.UseShellExecute = false;
myPro.StartInfo.RedirectStandardInput = true;
myPro.StartInfo.RedirectStandardOutput = true;
myPro.StartInfo.RedirectStandardError = true;
myPro.StartInfo.CreateNoWindow = true;
myPro.Start();
//如果調(diào)用程序路徑中有空格時,cmd命令執(zhí)行失敗,可以用雙引號括起來 ,在這里兩個引號表示一個引號(轉(zhuǎn)義)
string str = string.Format(@"""{0}"" {1} {2}", cmdExe, cmdStr, "&exit");
myPro.StandardInput.WriteLine(str);
myPro.StandardInput.AutoFlush = true;
myPro.WaitForExit();
result = true;
}
}
catch
{
}
return result;
}