Turn from: Http://blog.csdn.net/danjiewu/article/details/3176001?P_AVPASS=PHDGBITAVPASSP
Recently in a. Net c/S system, need to implement automatic update. Ms has provided a ClickOnce, very convenient, but not very accustomed to use, or decided to write a simple.
Automatic Updates are a few steps of file comparison, downloading, and starting a program, where file comparisons can either maintain the version number manually in the configuration file, or compare the MD5 value of the file, or the. NET can also use the assembly or file version number. Because of the fear of trouble, manual maintenance is not considered, the remaining two strengths, are provided for the choice. Download is relatively simple, HTTP, FTP, WebService can be selected. The startup program generally uses the System.Diagnostics.Process.Start to be possible, I use is appdomain.executeassembly.
To automatically generate file version information, need to have a service side, can be webservice and so on, I adopted the implementation of their own IHttpHandler, provide version information and download files.
Here is the class diagram and definition of updatehelper:
Getupdateinfos, Checkifneedupdate, DownloadFile is to get file version information, compare the file is the latest, download the role of the file, very simple. public static class updatehelper { public static updateinfo[] getupdateinfos () { httpwebrequest request = (HttpWebRequest) webrequest.create (Properties.Settings.Default.Server); request. method = "Get"; httpwebresponse response = (HttpWebResponse) request. GetResponse (); if (response. Statuscode == httpstatuscode.ok) { using (Stream Output = request. GetResponse (). GetResponseStream ()) { xmlserializer xml = new xmlserializer (typeof (UpdateInfo[)); return (updateinfo[]) XML. Deserialize (output); } } else { &nBsp; throw new exception ("Exception occurs on the server. "); } } public static bool Checkifneedupdate (updateinfo update) { if (! File.exists (update. FileName)) { return true; } else {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; switch (update. Versiontype) { case versiontype.fileversion: return Fileversioninfo.getversioninfo (update. FileName). Fileversion != update. Version; &NBSP;&NBSP;CASE&NBSP;VERSIONTYPE.MD5: using (filestream file = file.openread (update. FileName)) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; { return convert.tobase64string (MD5. Create (). ComputeHash (file)) != update. Version; } default: return false; } } } public static void update () { Updateinfo[] updates = updatehelper.getupdateinfos (); List<Thread> Downloadsthreads = new list<thread> ();