Feel used to, save, turn from: http://blog.csdn.net/gisfarmer/article/details/4437994
Now anything is a program has a corresponding upgrade program, if your program does not have a corresponding upgrade program, then you need to pay attention to. Your users are likely to lose!!! There are many examples of automatic upgrade on the Internet, a few days ago a friend was very distressed to tell me that its customers are gradually reduced (according to him, he is the customer because of his program upgrade is very troublesome, so many people gave up using its software), asked me how to do? Actually, he knows what to do. So... Friends! He made an automatic upgrade program for him. Just now an old friend on Csdn asked about it, so he shared the code with everyone.
Let's start with a few graphs:
Main principle (fairly simple):
Upgrade program must be a separate EXE, it is best not to tie up with your program (otherwise the upgrade program will not start). The main program exits----the upgrade program starts----The upgrade program accesses the upgrade profile of your site-----Read the information inside the profile-----Download-----The Upgrade program closes----The main program starts
Main code:
1. Read the configuration file:
1 Private voidGetupdateinfo ()2 {3 //Get Server information4WebClient client =NewWebClient ();5Doc =NewXmlDocument ();6 Try7 {8Doc. Load (client. OpenRead ("Http://192.168.3.43/update/update.xml"));9 //Doc. Load (client. OpenRead (Config.inireadvalue ("Update", "UpdateUrl", application.startuppath+ "//config.ini ") +"//update.xml "));Ten //Doc. Load (Application.startuppath + "//update.xml "); OneClient =NULL; A } - Catch - { the This. Labhtml.text ="Unable to get update file! Program Upgrade failed! "; - return; - } - if(doc = =NULL) + return; - //Analysis File + XmlNode node; A //get a list of files at stringRootPath = doc. selectSingleNode ("Product/filerootpath"). Innertext.trim (); -node = doc. selectSingleNode ("product/filelist"); - if(Node! =NULL) - { - foreach(XmlNode xninchnode. ChildNodes) - { in This. LISTVIEW1.ITEMS.ADD (NewListViewItem (New string[] - { toXn. attributes["Name"]. Value.tostring (), + NewWebfileinfo (ROOTPATH+XN. attributes["Name"]. Value.tostring ()). GetFileSize (). ToString (), - "---" the })); * } $ }Panax Notoginseng}
2. File Download:
/// <summary> ///Download File/// </summary> Public voidDownload () {FileStream fs=NewFileStream ( This. Strfile,filemode.create,fileaccess.write,fileshare.readwrite); Try { This. Objwebrequest = (HttpWebRequest) webrequest.create ( This. strURL); This. Objwebrequest.allowautoredirect =true;//int noffset = 0; LongNcount =0; byte[] buffer =New byte[4096];//4KB intNRECV =0;//number of bytes received This. Objwebresponse = (HttpWebResponse) This. Objwebrequest.getresponse (); Stream Recvstream= This. Objwebresponse.getresponsestream (); LongnMaxLength = (int) This. Objwebresponse.contentlength; if( This. bcheckfilesize && nMaxLength! = This. Nfilesize) { Throw NewException (string. Format ("Documents/"{0}/"is damaged and cannot be downloaded!", Path.getfilename ( This. strfile)) ); } if( This. Downloadfilestart! =NULL ) This. Downloadfilestart (NewDownloadfilestarteventargs ((int) (nmaxlength)) ; while(true) {Nrecv= Recvstream.read (buffer,0, buffer. Length); if(Nrecv = =0 ) Break; Fs. Write (Buffer,0, NRECV); Ncount+=Nrecv; //raises the download block completion event if( This. Downloadfileblock! =NULL ) This. Downloadfileblock (NewDownloadfileeventargs ((int) nMaxLength, (int) (ncount)) ; } recvstream.close (); //To raise a download completion event if( This. Downloadfilecomplete! =NULL ) This. Downloadfilecomplete ( This, Eventargs.empty); } finally{fs. Close (); } }
C # Write Automatic updater (GO)