zoukankan      html  css  js  c++  java
  • 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();            //等待进程结束
            }
        }
    }
  • 相关阅读:
    加解密工具类(含keystore导出pfx)
    Java使用数字证书加密通信(加解密/加签验签)
    关于javax.crypto.BadPaddingException: Blocktype错误的几种解决方法
    产生定长的随机数
    数字证书生成--加密密/加签验签
    随机指定范围内N个不重复的数
    sql2012还原sql2008备份文件语句
    uploadify API
    海量数据处理分析
    .net经验积累
  • 原文地址:https://www.cnblogs.com/littlehb/p/5782714.html
Copyright © 2011-2022 走看看