實現C/S程式的自動更新2

來源:互聯網
上載者:User

轉自:http://blog.csdn.net/danjiewu/article/details/3176001?P_AVPASS=PHDGBITAVPASSP


最近在做一個.Net C/S的系統,需要實現自動更新。MS已經提供了ClickOnce,很方便,但是用起來不太習慣,還是決定自己寫一個簡單的。

自動更新無非檔案比較、下載、啟動程式幾個步驟,其中檔案比較可以通過手動在設定檔中維護版本號碼,也可以比較檔案的MD5值,或者在.Net裡還可以用Assembly或檔案的版本號碼。因為怕麻煩,手動維護不考慮,剩下兩者各有所長,都提供了以供選擇。下載就比較簡單了,http、ftp、WebService都可以選擇。啟動程式一般用System.Diagnostics.Process.Start就可以,我用的是AppDomain.ExecuteAssembly。

要自動組建檔案版本資訊,需要有個服務端,可以是WebService等等,我採用的是自己實現IHttpHandler,提供檔案的版本資訊和下載。

下面是UpdateHelper的類圖和定義:

 

 

GetUpdateInfos、CheckIfNeedUpdate、DownloadFile分別是擷取檔案版本資訊、比較檔案是否是最新的、下載檔案的作用,很簡單。     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             {                 throw new Exception("Exception occurs on the server.");             }         }         public static bool CheckIfNeedUpdate(UpdateInfo update)         {             if (!File.Exists(update.FileName))             {                 return true;             }             else             {                 switch (update.VersionType)                 {                     case VersionType.FileVersion:                         return FileVersionInfo.GetVersionInfo(update.FileName).FileVersion != update.Version;                     case VersionType.MD5:                         using (FileStream file = File.OpenRead(update.FileName))                         {                             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>();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.