使用WebClient下载时,DownloadProgressChanged、DownloadFileCompleted这两个事件中如何获取文件名?
是循环下载多个文件,想分别显示每个文件下载的百分比,但是在这两个事件中不知道如何取文件名,求高手帮忙
------解决思路----------------------
下载多个,要分开几个线程,事件中没有文件名
------解决思路----------------------
给你写了一个例子。
using System;
using System.Linq;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var urls = new string[] {
"http://download.firefox.com.cn/releases/stub/official/zh-CN/Firefox-latest.exe" ,
"http://client01.pdl.wow.battlenet.com.cn/downloads/wow-installers/full/World-of-Warcraft-Setup-zhCN.exe"
};
foreach (var url in urls)
Download(url, Progress);
Console.ReadKey();
}
private static void Download(string url, Action<string, int> progress)
{
var p = url.Split(new char[] { '/', '\\' });
var filename = p.Last();
var web = new WebClient();
web.DownloadProgressChanged += (s, e) =>
{
progress(filename, e.ProgressPercentage);
};
web.DownloadFileAsync(new Uri(url), filename);
}
private static void Progress(string filename, int x)
{
Console.WriteLine("{0}............{1}%", filename, x);
}
}
------解决思路----------------------
Lamada 语法。