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>;
}
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[].ToString().Replace("(", "").Replace(")", "").Replace("%", "").Replace("s","");
if (rbraces1 == "OK")
{
rbraces1 = "";
}
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(); //等待进程结束
}
}
}
C#中使用aria2c进行下载并显示进度条的更多相关文章
- 【转】C#中使用aria2c进行下载并显示进度条
[转自] C#中使用aria2c进行下载并显示进度条 - 云平台知识库 - 博客园https://www.cnblogs.com/littlehb/p/5782714.html 正则表达式的生成网站: ...
- VC下载文件显示进度条
VC下载文件显示进度条 逗比汪星人2009-09-18上传 by Koma http://blog.csd.net/wangningyu http://download.csdn.net/deta ...
- VC下载文件 + 显示进度条
在codeproject里找了许久,发现这样一个VC下载文件并显示进度条的源码,于是添加了些中文注释: 1.下载线程函数: UINT DownloadFile(LPVOID pParam) { CWn ...
- idhttp post 上传或下载时显示进度条
通过 idhttp 带进度条上传演示一下,下载和上传原理差不多,说明一下下面例子中的的idhttp 是动态创建的 第一步:添加一个StatusBar或者gauge 进度条,这2个都可以.我用的是 st ...
- idhttp post 上传或下载时显示进度条(对接idhttp1.OnWork事件)
通过 idhttp 带进度条上传演示一下,下载和上传原理差不多,说明一下下面例子中的的idhttp 是动态创建的 第一步:添加一个StatusBar或者gauge 进度条,这2个都可以.我用的是 st ...
- libcurl开源库在Win32程序中使用下载文件显示进度条实例
一.配置工程引用libcurl库 #define CURL_STATICLIB #include "curl/curl.h" #ifdef _DEBUG #pragma comme ...
- Android 下载文件 显示进度条
加入两个权限 一个是联网,另一个是读写SD卡 <uses-permission android:name="android.permission.INTERNET">& ...
- 在WPF中实现图片一边下载一边显示
原文 在WPF中实现图片一边下载一边显示 当我们上网查看一个较大的图片时,浏览器能一边下载一边显示,这样用户体验是比较好的,但在WPF程序中,当我们通过如下方式显示一幅图片时: img.Source ...
- WPF下载远程文件,并显示进度条和百分比
WPF下载远程文件,并显示进度条和百分比 1.xaml <ProgressBar HorizontalAlignment="Left" Height="10&quo ...
随机推荐
- 8 定制10MINs 3
1. <div class="ui inverted red basic segment"> <h3 class="ui header"> ...
- 关于我的Android 博客
我是曹新雨,我为自己代言.现在的菜鸟,3年以后我就是大神.为自己加油.微信:aycaoxinyu 关于我的Android博客,都是我当初遇到困难,克服之后,写上去的.后来,有人加我微信,问我一些问题, ...
- 哪些工具能有效管理Azure Active Directory?
[TechTarget中国原创] 管理Azure Active Directory有四种常见的工具:Azure Web门户.Azure PowerShell.Azure命令行接口和Azure Mana ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目7
2014-03-20 03:35 题目:实现画图的Flood Fill操作. 解法:DFS和BFS皆可,但BFS使用的队列在时间复杂度上常数项比较大,速度略慢,所以我选了DFS.当然,如果图很大的话D ...
- python学习笔记十三:Flask demo
一.Flask简介 Flask 是一个 Python 实现的 Web 开发微框架.官网:http://flask.pocoo.org/ 二.Demo 1.代码结构 . ├── blog.py ├── ...
- 指定user镜像安装的磁盘
ironic node-update <node uuid> add properties/root_device='{"name":"/dev/sdb&qu ...
- OZ常见错误解决办法
执行成功 错误信息解决办法 libvirt.libvirtError: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No ...
- VC调试篇:减少运行时错误,中断所有异常
问题简述 我在Win7下写的MFC程序,想让它在winXP下运行.一般情况下,如果所有的依赖库都可以在XP下运行的话,那么在XP下运行时没问题的.但是,结果却... 本来程序在win7下运行得好好的, ...
- shell sort 排序大讨论
转自http://roclinux.cn 本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. === [正文开始]有时候学 ...
- CodeForces Round #515 Div.3 C. Books Queries
http://codeforces.com/contest/1066/problem/C You have got a shelf and want to put some books on it. ...