【转】C#中使用aria2c进行下载并显示进度条
【转自】
C#中使用aria2c进行下载并显示进度条 - 云平台知识库 - 博客园
https://www.cnblogs.com/littlehb/p/5782714.html
正则表达式的生成网站:
http://www.txt2re.com/index-csharp.php3
Aria2c下载地址:
https://github.com/aria2/aria2/releases/tag/release-1.34.0
保存的位置:程序运行目录/Download
Program.cs
static void Main(string[] args)
{
var url = "http://dldir1.qq.com/weixin/Windows/WeChatSetup.exe";
if (url != "")
{
var fileName = url.Substring(url.LastIndexOf("/") + );
var r = HttpDownLoad.DownloadFileByAria2(url, string.Format("{0}\\Download\\{1}", Directory.GetCurrentDirectory(), fileName));
Console.WriteLine(r);
Console.ReadLine();
}
}
HttpDownLoad.cs
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进行下载并显示进度条
正则表达式的生成网站: http://www.txt2re.com/index-csharp.php3 Aria2c下载地址: https://github.com/aria2/aria2/relea ...
- 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 ...
随机推荐
- Windows下安装logstash
1. 下载 https://www.elastic.co/downloads/logstash https://www.elastic.co/downloads/past-releases 2. 文档 ...
- 使用heap profiler进行内存占用分析
最近在项目中用到了google的heap profiler工具来分析内存占用,效果非常显著,因此在这里写一篇博客记录一下使用过程中遇到的一些问题. heap profiler依赖于tcmalloc,所 ...
- Android 自定义组件之 带有悬浮header的listview
最近做项目遇到一个需求,要做一个带有悬浮header的listview,即,当listview滑动时,header消失,静止时header浮现. 这个需求看似简单,实际做起来还是会遇到不少的困难,特此 ...
- js动态的给json对象添加新的元素
把{name:"xxx",age:"xxx"}变成{name:"xxx",age:"xxx",value:"x ...
- JAVA集合中的迭代器的遍历
JAVA中的迭代器,迭代实质上就是遍历,在JAVA中使用iterator()方法进行迭代.需要注意的是,iterator()方法的返回值是Iterator对象.Iterator对象有三个方法,hasN ...
- python的 pep8 规范(看完你会感谢我的!!!)
1 缩进与换行 每级缩进使用四个空格 2 限制每行的最大长度为79个字符 3 空行 顶层函数和类之间使用两个空行 类的方法之间用一个空行 在函数中使用空行表示不同逻辑段落 4 导入位于文件的顶部 5 ...
- [Unity Shader笔记]渲染路径--Forward渲染路径
[Unity Shader笔记]渲染路径--Forward渲染路径 (2014-04-22 20:08:25) 转载▼ 标签: shader unity renderingpath forward 游 ...
- OracleBulkCopy 修正帮
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Refle ...
- Tag recommendaion... 论文中的小例子,使用HOSVD算法推荐
本文内容来自于论文:Tag recommendations based on tensor dimensioanlity reduction 在社会标签系统中,存在三元关系,用户-物品-标签.这些数据 ...
- Ubuntu16 apt-get更换为阿里源
1.备份系统自带源 mv /etc/apt/sources.list /etc/apt/sources.list.bak 2.修改/etc/apt/sources.list文件 vim /etc/ap ...