关于系统的自动更新。近日有一情况是需要将java端后台最新版本的系统文件覆盖本地客户端,简称自动更新了。
本地会获取当前系统的版本号去请求后台java的接口数据。返回给我的是后台压缩包转的base64字节流。
客户端拿到新版本需要更新本地程序。
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(); //获取文件内的版本号 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; } } ////// 解压zip包覆盖更新 /// /// 接受更新包的字节信息 /// 覆盖的路径 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节中的配置内容 /// /// /// /// 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#如何实现自动更新本地程序的实例分析的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2493762.html