using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace prjDownLoad
{
class Program
{
static void Main(string[] args)
{
//Version3.0
//使用WebClient来下载
WebClient wc = new WebClient();
//注册当数据下载完成以后触发的事件
wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted);
//注册文件在下载过程中触发的事件
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
//开始异步下载数据
wc.DownloadDataAsync(new Uri("http://blog.sina.com.cn/dalishuishou"));
Console.ReadLine();
}
static void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
//打印下载的字节数
Console.WriteLine(string.Format("已经下载了{0}字节", e.BytesReceived));
//打印下载的百分比
Console.WriteLine(string.Format("已经完成的百分比:{0}%", e.ProgressPercentage));
}
static void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
FileStream fs = new FileStream("c:\\cc.txt", FileMode.Create, FileAccess.Write);
fs.Write(e.Result,0,e.Result.Length);
fs.Flush();
fs.Close();
Console.WriteLine("下载完成");
}
}
}