• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C#如何实现自动更新本地程序的实例分析

c# 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

关于系统的自动更新。近日有一情况是需要将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<span>本文来源gaodai#ma#com搞*!代#%^码$网*</span>.Settings[appSettingKey].Value;        }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C#如何实现自动更新本地程序的实例分析
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址