新聞中心
這篇文章將為大家詳細(xì)講解有關(guān)C#怎么實(shí)現(xiàn)自動更新本地程序,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
關(guān)于系統(tǒng)的自動更新。近日有一情況是需要將java端后臺最新版本的系統(tǒng)文件覆蓋本地客戶端,簡稱自動更新了。
本地會獲取當(dā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é)流了。
這個(gè)方法可以解決在同一個(gè)解決方案中有多個(gè)項(xiàng)目可以讀取到同一個(gè)項(xiàng)目下的App.config文件。
注意:其中有引用到的類庫, 這是是用來操作壓縮包的。
說下思路:第一步其實(shí)就是拿到壓縮包的字節(jié)流再保存到本地,第二步就是循環(huán)讀取壓縮包的文件替換本地的文件,完成本地系統(tǒng)的版本更新。
無論簡單與復(fù)雜,都需一步步向前方邁進(jìn)。
關(guān)于C#怎么實(shí)現(xiàn)自動更新本地程序就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享名稱:C#怎么實(shí)現(xiàn)自動更新本地程序-創(chuàng)新互聯(lián)
文章地址:http://www.dlmjj.cn/article/eissi.html