当前位置: 代码迷 >> C# >> 使用WebClient下载时,DownloadProgressChanged事件中怎么获取文件名
  详细解决方案

使用WebClient下载时,DownloadProgressChanged事件中怎么获取文件名

热度:85   发布时间:2016-05-05 04:12:37.0
使用WebClient下载时,DownloadProgressChanged事件中如何获取文件名?
使用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);
        }

    }

------解决思路----------------------
引用:
var web = new WebClient();
            web.DownloadProgressChanged += (s, e) =>
            {
                progress(filename, e.ProgressPercentage);
            };

这种注册事件的写法叫什么啊,菜鸟头一次见。
经常看到你回别人的帖子,感谢大神


Lamada 语法。