C#中使用aria2c进行下载并显示进度条

正则表达式的生成网站:

http://www.txt2re.com/index-csharp.php3

 Aria2c下载地址:

https://github.com/aria2/aria2/releases/tag/release-1.34.0

保存的位置:%appdata%

 

Program.cs

  static void Main(string[] args)
        {
            var url ="https://download.microsoft.com/download/E/4/1/E4173890-A24A-4936-9FC9-AF930FE3FA40/NDP461-KB3102436-x86-x64-AllOS-ENU.exe";
            var r=HttpDownLoad.DownloadFileByAria2(url, "c:\\NDP461-KB3102436-x86-x64-AllOS-ENU.exe");
            Console.WriteLine(r);
            Console.ReadLine();
        }

 

using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
{
    public class HttpDownLoad
    {
        /// <summary>
        /// 功能:使用Aria2c进行文件下载
        /// 作者:黄海
        /// 时间:2018-06-13
        /// </summary>
        /// <param name="url"></param>
        /// <param name="strFileName"></param>
        /// <returns></returns>
        public static bool DownloadFileByAria2(string url, string strFileName)
        {
            var tool = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\aria2-1.34.0-win-64bit-build1\\aria2c.exe";
            var fi = new FileInfo(strFileName);
            var command = " -c -s 10 -x 10  --file-allocation=none --check-certificate=false -d " + fi.DirectoryName + " -o " + fi.Name + " " + url;
            using (var p = new Process())
            {
                RedirectExcuteProcess(p, tool, command, (s, e) => ShowInfo(url,e.Data));
            }
            return File.Exists(strFileName) && new FileInfo(strFileName).Length>0;
        }
        private static void ShowInfo(string url,string a)
        {
            if (a == null) return;

            const string re1 = ".*?"; // Non-greedy match on filler
            const string re2 = "(\\(.*\\))"; // Round Braces 1

            var r = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            var m = r.Match(a);
            if (m.Success)
            {
                var rbraces1 = m.Groups[1].ToString().Replace("(", "").Replace(")", "").Replace("%", "").Replace("s","0");
                if (rbraces1 == "OK")
                {
                    rbraces1 = "100";
                }
                Console.WriteLine(DateTime.Now.ToString().Replace("/","-")+"    "+url+"    下载进度:"+rbraces1+"%");
            }
        }

        /// <summary>
        /// 功能:重定向执行
        /// </summary>
        /// <param name="p"></param>
        /// <param name="exe"></param>
        /// <param name="arg"></param>
        /// <param name="output"></param>
        private static void RedirectExcuteProcess(Process p, string exe, string arg, DataReceivedEventHandler output)
        {
            p.StartInfo.FileName = exe;
            p.StartInfo.Arguments = arg;

            p.StartInfo.UseShellExecute = false;    //输出信息重定向
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.OutputDataReceived += output;
            p.ErrorDataReceived += output;

            p.Start();                    //启动线程
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
            p.WaitForExit();            //等待进程结束
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值