C # compile an automatic update program

Source: Internet
Author: User

Now, all programs have corresponding upgrade programs. If your programs do not have corresponding upgrade programs, you need to pay attention to them. Your users may be lost !!! There are also many examples of automatic upgrades on the Internet. A friend complained to me a few days ago that their customers were gradually decreasing (according to him, he is a customer who has trouble upgrading his program, so many people give up using the software.) ask me what to do? Actually, he knows what to do? So... friends! Then an Automatic Upgrade Program is created for him. Today, an old friend on csdn also asked about the issue, so he shared the Code with everyone.

First, let's look at the following figures:

 

 

Main principle (relatively simple ):

The upgrade program must be a separate EXE, so it is best not to bind it with your program (otherwise the upgrade program cannot be started ). Exit the main program ---- Upgrade Program start ---- Upgrade Program access your website's upgrade configuration file ----- read the information in the configuration file ----- download ----- Upgrade Program closed ---- start the main program

Main Code:

1. Read the configuration file:

Private void getupdateinfo () <br/>{< br/> // obtain Server Information <br/> WebClient client = new WebClient (); <br/> Doc = new xmldocument (); <br/> try <br/>{< br/> Doc. load (client. openread ("http: // 192.168.3.43/update. XML "); <br/> // Doc. load (client. openread (config. inireadvalue ("Update", "updateurl", application. startuppath + "// config. ini ") +" // update. XML "); <br/> // Doc. load (application. startuppath + "// updat E. XML "); <br/> client = NULL; <br/>}< br/> catch <br/>{< br/> This. labhtml. TEXT = "unable to get the Update file! An error occurred while upgrading the program! "; <Br/> return; <br/>}< br/> If (Doc = NULL) <br/> return; <br/> // analyze the file <br/> xmlnode node; <br/> // obtain the file list <br/> string rootpath = Doc. selectsinglenode ("Product/filerootpath "). innertext. trim (); <br/> node = Doc. selectsinglenode ("Product/filelist"); <br/> If (node! = NULL) <br/>{< br/> foreach (xmlnode Xn in node. childnodes) <br/>{< br/> This. listview1.items. add (New listviewitem (New String [] <br/>{< br/> XN. attributes ["name"]. value. tostring (), <br/> New webfileinfo (rootpath + Xn. attributes ["name"]. value. tostring ()). getfilesize (). tostring (), <br/> "---" <br/>}); <br/>}< br/>}

2. File Download:

/// <Summary> <br/> // download the object <br/> /// </Summary> <br/> Public void download () <br/>{< br/> filestream FS = new filestream (this. strfile, filemode. create, fileaccess. write, fileshare. readwrite); <br/> try <br/> {<br/> This. objwebrequest = (httpwebrequest) webrequest. create (this. strurl); <br/> This. objwebrequest. allowautoredirect = true; <br/> // int noffset = 0; <br/> long ncount = 0; <br/> byte [] buffer = new Byte [4096]; // 4 kb <br/> int nrecv = 0; // number of bytes received <br/> This. objwebresponse = (httpwebresponse) This. objwebrequest. getresponse (); <br/> stream recvstream = This. objwebresponse. getresponsestream (); <br/> long nmaxlength = (INT) This. objwebresponse. contentlength; <br/> If (this. bcheckfilesize & nmaxlength! = This. nfilesize) <br/>{< br/> throw new exception (string. Format ("file/" {0}/"is damaged and cannot be downloaded! ", Path. getfilename (this. strfile); <br/>}< br/> If (this. downloadfilestart! = NULL) <br/> This. downloadfilestart (New downloadfilestarteventargs (INT) nmaxlength); <br/> while (true) <br/>{< br/> nrecv = recvstream. read (buffer, 0, buffer. length); <br/> If (nrecv = 0) <br/> break; <br/> FS. write (buffer, 0, nrecv); <br/> ncount + = nrecv; <br/> // triggers the download block completion event <br/> If (this. downloadfileblock! = NULL) <br/> This. downloadfileblock (New downloadfileeventargs (INT) nmaxlength, (INT) ncount); <br/>}< br/> recvstream. close (); <br/> // triggers the download completion event <br/> If (this. downloadfilecomplete! = NULL) <br/> This. downloadfilecomplete (this, eventargs. empty); <br/>}< br/> finally <br/> {<br/> FS. close (); <br/>}< br/>

You are welcome to reprint it. Please indicate the source for reprinting :)

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.