首页 > Windows开发 > 详细

C#调用cmd并使用IDM下载文件

时间:2017-05-25 09:27:23      阅读:1082      评论:0      收藏:0      [点我收藏+]
 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命令执行失败,可以用双引号括起来

C#调用cmd并使用IDM下载文件

原文:http://www.cnblogs.com/kangjing/p/6901969.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!