使用WebClient实现文件下载

标签:
杂谈 |
分类: c#方面的总结 |
- using
System; - using
System.Collections.Generic; - using
System.ComponentModel; - using
System.Data; - using
System.Drawing; - using
System.Text; - using
System.Windows.Forms; - using
System.Net; - using
System.IO; - namespace
wiindowsFormsApplication - {
-
partial classForm1 : Form -
{ -
Form1() -
{ -
InitializeComponent(); -
= "http://dl-sh-ocn-1.pchome.net/0d/bx/koomail50b8.rar";@ -
} -
WebClient webClient = WebClient(); -
void btn_down_Click( objectsender, EventArgs e) -
{ -
-
(webClient.IsBusy) //是否存在正在进行中的Web请求 -
{ -
webClient.CancelAsync(); -
} -
-
webClient.DownloadProgressChanged += DownloadProgressChangedE ventHandler(webClient_DownloadProgressChanged); -
webClient.DownloadFileCompleted+= AsyncCompletedEventHandl er(webClient_DownloadFileCompleted); -
-
webClient.DownloadFileAsync( Uri( this.textBox1.Text),"aa.rar"); -
} -
-
void webClient_DownloadProgressChanged( objectsender, DownloadProgressChangedE ventArgs e) -
{ -
= e.ProgressPercentage; -
= "%";e.ProgressPercentage.ToString() + -
= string.Format("正在下载文件,完成进度{0}/{1}(字节)" -
,e.BytesReceived -
,e.TotalBytesToReceive); -
} -
void webClient_DownloadFileCompleted( objectsender, AsyncCompletedEventArgs e) -
{ -
(e.Cancelled) -
MessageBox.Show( -
-
MessageBox.Show( -
} -
void btn_cancel_Click( objectsender, EventArgs e) -
{ -
-
-
} -
} - }
运行界面: