We do the program, there will inevitably be version upgrade, which requires the program has automatic version upgrade features. Then look at how I implemented the program to update automatically. Directly on the code:
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Reflection; usingSystem.IO; usingSystem<a href="http://lib.csdn.net/base/dotnet" class='Replace_word'title=". NET Knowledge Base"target='_blank'style='color: #df3434; font-weight:bold;'>.NET</a>; usingSystem.Xml; namespaceUpdate {/// <summary> ///events triggered by update completion/// </summary> Public Delegate voidupdatestate (); /// <summary> ///program Updates/// </summary> Public classSoftupdate {Private stringdownload; Private Const stringUpdateUrl ="http://www.csdn<a href="http//lib.csdn.net/base/dotnet "class= ' Replace_word ' title=". NET Knowledge Base "target= ' _blank ' style= ' color: #df3434; Font-weight:bold; ' >.net</a>/update.xml ";//XML file address for upgrade configuration #regionconstructor function Publicsoftupdate () {}/// <summary> ///program Updates/// </summary> /// <param name= "file" >the file to update</param> PublicSoftupdate (stringFilestringsoftname) { This. LoadFile =file; This. Softname =Softname; } #endregion #regionPropertyPrivate stringLoadFile; Private stringNewverson; Private stringSoftname; Private BOOLisupdate; /// <summary> ///or if you need to update/// </summary> Public BOOLIsupdate {Get{checkupdate (); returnisupdate; } } /// <summary> ///to check for updated files/// </summary> Public stringLoadFile {Get{returnLoadFile;} Set{LoadFile =value;} } /// <summary> ///New Assembly version/// </summary> Public stringNewverson {Get{returnNewverson;} } /// <summary> ///name of the upgrade/// </summary> Public stringSoftname {Get{returnSoftname;} Set{Softname =value;} } #endregion /// <summary> ///events triggered when the update is complete/// </summary> Public Eventupdatestate Updatefinish; Private voidIsfinish () {if(Updatefinish! =NULL) Updatefinish (); } /// <summary> ///Download Updates/// </summary> Public voidUpdate () {Try { if(!isupdate)return; WebClient WC=NewWebClient (); stringfilename =""; stringExten = download. Substring (download. LastIndexOf (".")); if(Loadfile.indexof (@"/") == -1) filename="Update_"+ path.getfilenamewithoutextension (loadFile) +Exten; Elsefilename= Path.getdirectoryname (loadFile) +"//update_"+ path.getfilenamewithoutextension (loadFile) +Exten; Wc. DownloadFile (download, filename); Wc. Dispose (); Isfinish (); } Catch { Throw NewException ("An error occurred in the update and the network connection failed! "); } } /// <summary> ///Check if updates are required/// </summary> Public voidcheckupdate () {Try{WebClient WC=NewWebClient (); Stream Stream=WC. OpenRead (UpdateUrl); XmlDocument xmldoc=NewXmlDocument (); Xmldoc.load (stream); XmlNode List= Xmldoc.selectsinglenode ("Update"); foreach(XmlNode nodeinchlist) { if(node. Name = ="Soft"&& node. attributes["Name"]. Value.tolower () = =Softname.tolower ()) { foreach(XmlNode XMLinchnode) { if(XML. Name = ="Verson") Newverson=XML. InnerText; ElseDownload=XML. InnerText; }}} Version ver=NewVersion (Newverson); Version Verson=Assembly.LoadFrom (loadFile). GetName (). Version; intTM =Verson.compareto (ver); if(TM >=0) Isupdate=false; Elseisupdate=true; } Catch(Exception ex) {Throw NewException ("Update Error, please verify that the network connection is correct and try again! "); } } /// <summary> ///get the file to update/// </summary> /// <returns></returns> Public Override stringToString () {return This. LoadFile; } } }
Compile the code into a class library file, and pass the program reference to OK. The parameters passed in are already commented. The following is the updated XML file class, uploaded to the space above, to get the address of the XML file.
<?XML version= "1.0" encoding= "Utf-8"?> <Update> <SoftName= "Blogwriter"> <Verson>1.0.1.2</Verson> <DownLoad>Http://www.csdn.net/BlogWrite.rar</DownLoad> </Soft> </Update>
Program Update call Method: 1, first refer to the above DLL. 2. Call the method code as follows
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.text;using system.windows.forms;using system.io;using system.threading;using System.Net;using system.xml;using update;namespace updatetest{public partial class Form1:form {public Form1 () { InitializeComponent (); Checkupdate (); } public void Checkupdate () {softupdate app = new Softupdate (Application.executablepath, "BLOGWR Iter "); App. Updatefinish + = new Updatestate (app_updatefinish); try {if (app. Isupdate && MessageBox.Show ("Check to the new version, is it updated?") "," Update ", Messageboxbuttons.yesno, messageboxicon.question) = = Dialogresult.yes) {Th Read update = new Thread (The new ThreadStart (app. Update)); Update. Start (); }} catch (Exception ex) { MessageBox.Show (ex. Message); }} void App_updatefinish () {MessageBox.Show ("The update is complete, please restart the program!") "," Update ", MessageBoxButtons.OK, MessageBoxIcon.Information); } }}
Well, the whole program ends here.
Version upgrade update for C # implementation program