正则表达式的生成网站:
Aria2c下载地址:
保存的位置:%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 { ////// 功能:使用Aria2c进行文件下载 /// 作者:黄海 /// 时间:2018-06-13 /// /// /// ///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+"%"); } } /// /// 功能:重定向执行 /// /// /// /// /// 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(); //等待进程结束 } }}