C # implements the download function, which can be used for automatic software update

Source: Internet
Author: User

Previously written in the Baidu document, transferred here

Software:

Code Download:

http://twzy.ys168.com/ in the code download folder

//Code:usingSystem;usingSystem.ComponentModel;usingSystem.Net;usingSystem.Threading;usingSystem.Windows.Forms;namespacedownfile{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        stringNetPath ="Http://wlkt.zufe.edu.cn/Skyclass/C91/Courseware/Bbs/080717071122.mp3";//files that need to be downloaded        stringFilePath ="d:\\just One last Dance.mp3";//path to File save        Delegate voidDeldownfilehandler (intTotalnum,intNumintproc);//used to indicate file information (file size, current download quantity, percentage of current download)        Delegate voidDelcomdownfilehandler (BOOLiscompleted);//after the file download is completeWebClient WC=NewWebClient (); Private voidButton1_Click (Objectsender, EventArgs e) {WC. DownloadProgressChanged+=NewDownloadprogresschangedeventhandler (wc_downloadprogresschanged); Wc. Downloadfilecompleted+=NewAsynccompletedeventhandler (wc_downloadfilecompleted); progressBar1.Value=0; Label1. Text=""; Button2. Enabled=true; Button1. Enabled=false; //start with ThreadThread td =NewThread (NewThreadStart (loadFile)); Td. IsBackground=true; Td.        Start (); }        //Thread Start        Private voidLoadFile () {if(WC.) IsBusy) {WC.            CancelAsync (); } WC. DownloadFileAsync (NewUri (NetPath), FilePath); }         //events that are triggered during the update (controls cannot be manipulated directly in the thread and therefore executed with a delegate)        voidWc_downloadprogresschanged (Objectsender, DownloadProgressChangedEventArgs e) {             This. Invoke (NewDeldownfilehandler (Processshow),New Object[] { (int) E.totalbytestoreceive, (int) e.bytesreceived, e.progresspercentage}); }        //delegate Execution (progress bar and character id)        Private voidProcessshow (intTotalnum,intNumintproc) {Progressbar1.maximum=Totalnum; progressBar1.Value=num; Label1. Text="currently downloading:"+ (num/(1024x768)). ToString () +"kb/"+ (Totalnum/1024x768). ToString () +"KB"+ Proc. ToString () +"%"; }        //events that are triggered after the update is completed        voidWc_downloadfilecompleted (Objectsender, AsyncCompletedEventArgs e) {            if(e.cancelled) This. Invoke (NewDelcomdownfilehandler (Comdownfile),New Object[] {false }); Else                 This. Invoke (NewDelcomdownfilehandler (Comdownfile),New Object[] {true }); }        Private voidComdownfile (BOOLiscompleted) {            if(iscompleted) Label1. Text="Download Complete"; ElseLabel1. Text="Download Cancel"; progressBar1.Value=0; Button1. Enabled=true; Button2. Enabled=false; }        //after clicking Cancel        Private voidButton2_Click (Objectsender, EventArgs e) {WC.            CancelAsync (); Wc.        Dispose (); }    }}

The file path in the code has been invalidated, please modify it yourself

C # implements the download function, which can be used for automatic software update

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.