码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-05-25 10:07:13      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:com   class   文件   调用   text   toe   download   static   sql   

 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下载文件

标签:com   class   文件   调用   text   toe   download   static   sql   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!