Realize the automatic update of C/S program 2

Source: Internet
Author: User

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> ();

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.