登录  /  注册

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

黄舟
发布: 2017-08-08 14:13:15
原创
2180人浏览过

关于系统的自动更新。近日有一情况是需要将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#如何实现自动更新本地程序的实例分析的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号