1. 程式人生 > >C#自動更新本地程序

C#自動更新本地程序

字節 xtra 版本 jobject key filename map images with

關於系統的自動更新。近日有一情況是需要將java端後臺最新版本的系統文件覆蓋本地客戶端,簡稱自動更新了。

本地會獲取當前系統的版本號去請求後臺java的接口數據。返回給我的是後臺壓縮包轉的base64字節流。

客戶端拿到新版本需要更新本地程序。

    if (UpdateSystem(Path.Combine(Application.StartupPath, "Version.txt"), Path.Combine(Application.StartupPath, "u.zip")))
            {
                Application.Exit();
            }
     /// <summary>
        /// 讀取本地版本請求更新
        /// </summary>
        /// <param name="document">讀取的文件信息</param>
        /// <param name="zipPath">返回zip包本地路徑</param>
        /// <returns></returns>
        private bool UpdateSystem(string document, string zipPath)
        {
            
try { Dictionary<string, string> postDic = new Dictionary<string, string>(); //獲取文件內的版本號 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數據 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; } } /// <summary> /// 解壓zip包覆蓋更新 /// </summary> /// <param name="bytes">接受更新包的字節信息</param> /// <param name="zpath">覆蓋的路徑</param> 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); } /// <summary> /// 獲取配置文件中的appSettings節中的配置內容 /// </summary> /// <param name="appSettingKey"></param> /// <param name="message"></param> /// <returns></returns> 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());

這裏是拿到的字節流了。

技術分享

技術分享

這個方法可以解決在同一個解決方案中有多個項目可以讀取到同一個項目下的App.config文件。

註意:其中有引用到的類庫,技術分享 這是是用來操作壓縮包的。

說下思路:第一步其實就是拿到壓縮包的字節流再保存到本地,第二步就是循環讀取壓縮包的文件替換本地的文件,完成本地系統的版本更新。

無論簡單與復雜,都需一步步向前方邁進。

C#自動更新本地程序