码迷,mamicode.com
首页 > Web开发 > 详细

WebClient异步下载文件

时间:2014-08-24 23:44:53      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:http   os   文件   for   ar   line   new   sp   ad   


namespace ConsoleAppSyncDownload
{
    class Program
    {

        static void Main(string[] args)
        {
            WebClient webClient = new WebClient();
            //Console.Write("输入下载文件地址:");
            //var s = Console.ReadLine();
            Console.WriteLine("是否开始下载(Y/N)");
            if (Console.ReadLine() == "Y")
            {
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);

                webClient.DownloadFileAsync(new Uri("http://cd001.www.duba.net/duba/install/2011/ever/kavsetup140818_99_50.exe"), "0818_99_50.exe");

            }
            Console.Read();
        }

        private static void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                Console.WriteLine("下载被取消");
            }
            else
            {
                Console.WriteLine("下载完成");
            }
        }

        private static void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
          
            Console.WriteLine(string.Format("正在下载文件,完成进度{0}/{1}(字节){2}",
                e.BytesReceived, e.TotalBytesToReceive,e.ProgressPercentage.ToString() + "%"));
        }
    }
}

WebClient

WebClient异步下载文件

标签:http   os   文件   for   ar   line   new   sp   ad   

原文地址:http://www.cnblogs.com/liucyi/p/3933665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!