十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章將為大家詳細講解有關(guān)C#怎么實現(xiàn)自動更新本地程序,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
天門ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
關(guān)于系統(tǒng)的自動更新。近日有一情況是需要將java端后臺最新版本的系統(tǒng)文件覆蓋本地客戶端,簡稱自動更新了。
本地會獲取當前系統(tǒng)的版本號去請求后臺java的接口數(shù)據(jù)。返回給我的是后臺壓縮包轉(zhuǎn)的base64字節(jié)流。
客戶端拿到新版本需要更新本地程序。
if (UpdateSystem(Path.Combine(Application.StartupPath, "Version.txt"), Path.Combine(Application.StartupPath, "u.zip"))) { Application.Exit(); }
////// 讀取本地版本請求更新 /// /// 讀取的文件信息 /// 返回zip包本地路徑 ///private bool UpdateSystem(string document, string zipPath) { try { Dictionary postDic = new Dictionary (); //獲取文件內(nèi)的版本號 if(File.Exists(document)) { postDic.Add("version", File.ReadAllText(document).Trim()); } else { postDic.Add("version", "0"); } string postJson = JsonConvert.SerializeObject(postDic); string url = GetAppSettingValue("serverUrl") + "parkClient/parkClientUpdate"; //返回的json數(shù)據(jù) JObject obj = (JObject)JsonConvert.DeserializeObject(PostData(postJson, url)); string newVersion = obj["version"].ToString(); if (!String.IsNullOrWhiteSpace(newVersion)) { byte[] bytesFile = Convert.FromBase64String(obj["byteArray"].ToString()); if (obj["clientMD5"].ToString() == BitConverter.ToString( new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(bytesFile)).Replace("-", "")) { ZipCoverage(bytesFile, zipPath); File.WriteAllText(document, newVersion); } } return true; } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } } /// /// 解壓zip包覆蓋更新 /// /// 接受更新包的字節(jié)信息 /// 覆蓋的路徑 private void ZipCoverage(byte[] bytes, string zpath) { File.WriteAllBytes(zpath, bytes); using (ZipArchive archive = ZipFile.OpenRead(zpath)) { string file = null; foreach (ZipArchiveEntry entry in archive.Entries) { if (!entry.FullName.EndsWith("/")) { file = Path.Combine(Application.StartupPath, entry.FullName); if (File.Exists(file)) { File.Delete(file); } } } } ZipFile.ExtractToDirectory(zpath, Application.StartupPath); } ////// 獲取配置文件中的appSettings節(jié)中的配置內(nèi)容 /// /// /// ///private string GetAppSettingValue(string appSettingKey) { ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = @"TDH.Parking.Client.exe.config" }; return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None).AppSettings.Settings[appSettingKey].Value; }
byte[] bytesFile = Convert.FromBase64String(obj["byteArray"].ToString());
這里是拿到的字節(jié)流了。
這個方法可以解決在同一個解決方案中有多個項目可以讀取到同一個項目下的App.config文件。
注意:其中有引用到的類庫, 這是是用來操作壓縮包的。
說下思路:第一步其實就是拿到壓縮包的字節(jié)流再保存到本地,第二步就是循環(huán)讀取壓縮包的文件替換本地的文件,完成本地系統(tǒng)的版本更新。
無論簡單與復雜,都需一步步向前方邁進。
關(guān)于C#怎么實現(xiàn)自動更新本地程序就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。