WinForm download file and display download progress sample
The code is as follows |
Copy Code |
<summary> Show progress </summary> <param name= "Val" ></param> private void Progressbar_value (int val) { progressBar1.Value = val; Label1. Text = val. ToString () + "%"; } <summary> Download files </summary> <param name= "url" ></param> <param name= "SaveFile" ></param> <param name= "DownloadProgressChanged" ></param> <param name= "downloadfilecompleted" ></param> private void DownloadFile (string url, string savefile, action<int> downloadprogresschanged, Action downloadfilecompleted) { WebClient client = new WebClient (); if (downloadprogresschanged!= null) { Client. DownloadProgressChanged + = Delegate (object sender, DownloadProgressChangedEventArgs e) { This. Invoke (downloadprogresschanged, e.progresspercentage); }; } if (downloadfilecompleted!= null) { Client. downloadfilecompleted + = Delegate (object sender, AsyncCompletedEventArgs e) { This. Invoke (downloadfilecompleted); }; } Client. DownloadFileAsync (new Uri (URL), savefile); } delegate void Action (); . NET Framework 2.0 Custom Delegate action ///<summary> ///Click to download ///</summary> ///<param name= "sender" ></PARAM> ///& Lt;param name= "E" ></param> private void Button1_Click (object sender, EventArgs e) { & nbsp DownloadFile ("Http://www.111cn.net/update.zip", @ "F:update.zip", progressbar_value, NULL); } |