当地会猎取当前体系的版本号去要求背景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#怎样完成自动更新当地顺序的实例剖析的细致内容,更多请关注ki4网别的相干文章!