Realization function: Update user's software through the website, need to be networked, also can update LAN user software through local website.
Fundamental implementation: 1. A website (I use my own www.aq36.xyz, Local can, can use localhost) and then run the update.exe->{ Get the URL via Update.xml, then check the comparison of the current version number and the Server.xml maximum version number of the website. Then decide whether to update}.
2. Update to download the zip file, unzip the replacement and delete.
3. Update the Update.xml document version information, with the new end.
Main code:
1. Parse the XML file:
/// <summary> ///Loading and parsing XML/// </summary> Private voidLoadXml () {XmlDocument document=NewXmlDocument (); Try{document. Load (UpdateUrl+"Server.xml"); XmlNode node= document. selectSingleNode ("//updates"); //get the latest version numberVersion = Convert.ToInt32 (node. attributes["version"]. Value); //all need to upgrade file size, file list, SQL listXmlNodeList updatelist = document. SelectNodes ("//updates//update"); foreach(XmlNode Ninchupdatelist) { LongTempversion = Convert.ToInt32 (n.attributes["version"]. Value); LongTempsize = Convert.toint64 (n.attributes["size"]. Value); if(Tempversion > Localversion && tempversion <=version) {Allsize+=tempsize; Versions. ADD (Tempversion.tostring ()); //get the list of upgrade filesXmlNodeList fileList = N.selectnodes ("//update[@version = '"+ Tempversion +"']//files//file"); foreach(XmlNode N1inchfileList) {files. ADD (N1. InnerText); } //gets the list of SQL statements executedXmlNodeList sqllist = N.selectnodes ("//update[@version = '"+ Tempversion +"']//sqls//sql"); foreach(XmlNode N2inchsqllist) {Sqls. Add (n2. InnerText); } //Upgrade Prompt InformationXmlNodeList msglist = N.selectnodes ("//update[@version = '"+ Tempversion +"']//msg"); foreach(XmlNode N3inchmsglist) {msg+=string. Format (CultureInfo.InvariantCulture,"version "{0}" {1}",New Object[] {tempversion, N3. Innertext.replace ("\r\n\t\t","") }) +"\ r \ n"; } } } } Catch(Exception e) {//Console.WriteLine (e.message);MessageBox.Show ("failed to connect to upgrade server, please try again! ","Error Hints", MessageBoxButtons.OK, Messageboxicon.error); } }
2. Download the file:
Private voidDownloadFile (intindex) { Try{Downindex++; FileName=Files[index]; //labelnow.text = "Start download version" "+ Versions[index" + "" "+ filename;Labelall.text =string. Format (CultureInfo.InvariantCulture,"upgrade Progress {0}/{1} [{2}]",New Object[] {downindex, files. Count, This. Convertsize (Allsize)}); Pbnow.value=0; Downwebclient.downloadfileasync (NewUri (updateurl + filename), Application.startuppath +"/temp/"+filename); } Catch(WebException exception) {MessageBox.Show (exception. Message,"Error Hints", MessageBoxButtons.OK, Messageboxicon.error); } }
3. Start the Upgrade:
Private voidstartupdate () {if(Localversion >=version) {updatecompleted (0); return; } This. downwebclient.downloadprogresschanged + =NewDownloadprogresschangedeventhandler (downloadprogresschanged); This. downwebclient.downloadfilecompleted + =NewAsynccompletedeventhandler (downloadfilecompleted); if( This. allsize = =0L|| Files = =NULL|| Files. Count = =0) { This. Updatecompleted (0); } if(Files! =NULL&& files. Count >0) {DownloadFile (0); } }
4. Download complete and change Update.xml
Private voidDownloadfilecompleted (ObjectWcsender, AsyncCompletedEventArgs ex) { if(ex. Error! =NULL) {MessageBox.Show (ex. Error.message,"Error Hints", MessageBoxButtons.OK, Messageboxicon.hand); } Else { if(File.exists (Application.startuppath +@"\"+ This. FileName)) {File.delete (Application.startuppath+@"\"+ This. FileName); } file.move (Application.startuppath+@"\temp\"+ This. FileName, Application.startuppath +@"\"+ This. FileName); NewFastzip (). Extractzip (Application.startuppath +@"\"+ This. FileName, Application.startuppath +@"\",""); File.delete (Application.startuppath+@"\"+ This. FileName); This. downedsize + = This. FileSize; if( This. files. Count > This. Downindex) { This. DownloadFile ( This. Downindex); } Else{XmlTextWriter writer=NewXmlTextWriter (Application.startuppath +@"\update.xml",NULL) {Formatting=formatting.indented, Indentation=4 }; Writer. WriteStartDocument (); Writer. WriteStartElement ("Update"); Writer. WriteStartElement ("ProcessName"); Writer. WriteString ( This. ProcessName); Writer. WriteEndElement (); Writer. WriteStartElement ("version"); Writer. WriteString ( This. Version. ToString ()); Writer. WriteEndElement (); Writer. WriteStartElement ("URL"); Writer. WriteString ( This. UpdateUrl); Writer. WriteEndElement (); Writer. WriteEndElement (); Writer. WriteEndDocument (); Writer. Flush (); Writer. Close (); if(!string. IsNullOrEmpty ( This. msg)) {MessageBox.Show ( This. MSG,"Upgrade Tips", MessageBoxButtons.OK, Messageboxicon.asterisk); } This. Updatecompleted (1); } } }
With the new progress bar:
Before update:
After the update:
Some document code plots:
1. Local Update.xml
2. Website Server.xml
Code rough, just a simple demo, simple reference, there are errors in time to contact me.
Baidu Net disk source Add File:http://pan.baidu.com/s/1qYe2Vgg.
updating and modifying an. exe program via the Web