public static void RunCmd(string cmd) { string CmdPath = @"C:\Windows\System32\cmd.exe"; cmd = cmd.Trim().TrimEnd(‘&‘) + "&exit";//要加exit命令,否则后面调用ReadtoEnd()命令会假死 using (Process p = new Process()) { p.StartInfo.FileName = CmdPath; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine(cmd); p.StandardInput.AutoFlush = true; p.WaitForExit(); p.Close(); } }
调用:
string sIDMPath = @"E:\Software\idman_lv\IDM\IDMan.exe"; string sDownloadPath="http://pan.baidu.com/s/...";//网盘链接 string sOutPutPath = @"D:\TempFolder\"; string sFileName= Guid.NewGuid().ToString()+".pdf"; string sCmd = string.Format(@"{0} /d ""{1}"" /p ""{2}"" /f ""{3}""", sIDMPath, sDownloadPath, sOutPutPath, sFileName); CmdBox.AppendText(sCmd + "\n"); SqlProcs.RunCmd(sCmd);
注释:如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来
时间: 2024-10-12 17:29:36