前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自动更新程序GeneralUpdate

自动更新程序GeneralUpdate

作者头像
JusterZhu
发布2022-12-07 17:29:39
1.4K0
发布2022-12-07 17:29:39
举报
文章被收录于专栏:JusterZhuJusterZhu

描述:

GeneralUpdate是基于Autoupater进行升级开发。有人会奇怪为什么会改名称,稍微解释一下是因为在nuget上有重名的项目再者就是新版本更新功能不仅限于wpf程序的更新。将更新的核心部分抽离出来方便应用于多种项目当中目前适用于wpf,控制台应用,winfrom。相比以前更方便的是不需要在过分关注源码可直接通过nuget直接使用。如果有任何使用问题可以在Github的issues上进行提问我会每周统一找时间解决并解答bug或者问题。或者联系文章末尾的联系方式会有人解答。

Nuget地址:https://www.nuget.org/packages/GeneralUpdate.Core/

GitHub地址:https://github.com/WELL-E/AutoUpdater GeneralUpdate版本在 Branch:Autoupdate2。

issues:https://github.com/WELL-E/AutoUpdater/issues

新增特性速览:

1.简洁启动代码 如下:Launch1 Launch2

名称

类型

备注

UpdateOption.Format

配置参数

更新包的压缩格式(目前只支持zip)

UpdateOption.MainApp

配置参数

更新完成后需要启动的主程序名称

DownloadStatistics

事件

更新包下载通知事件

ProgressChanged

事件

更新进度通知事件

Strategy<>()

方法

策略注入

RemoteAddress()

方法

远程地址配置,如果没有则传入args[]

Launch

方法

启动更新

GeneralUpdateBootstrap

更新引导类

2.新增Strategy(更新策略),更新策略是开放出来让大家可以在不改动源码的情况下自由扩展更新方式将不会仅限于默认的更新策略。

3.更新本地文件时,会有更新通知事件。明确的告知更新文件总数和当前更新到第几个文件

4.新增更新状态 Check(检查更新),Donwload(下载更新包),Updatefile(更新文件),Done(更新完成),Fail(更新失败)。开发者可以直接通过一系列枚举值直接判断当前运行状态做出相应的处理

5.新增若干启动配置参数的验证

代码语言:javascript
复制
  if (args != null)
            {
                if (args.Length == 0)
                {
                    throw new NullReferenceException("Args does not contain any elements.");
                }

                if (args.Length > elementNum)
                {
                    throw new Exception($"The number of args cannot be greater than { elementNum }");
                }
            }

            if (string.IsNullOrWhiteSpace(PacketName))
            {
                throw new NullReferenceException("packet name not set");
            }

            if (string.IsNullOrWhiteSpace(DownloadPath))
            {
                throw new NullReferenceException("download path not set");
            }

            if (string.IsNullOrWhiteSpace(InstallPath))
            {
                throw new NullReferenceException("install path not set");
            }

            if (string.IsNullOrWhiteSpace(MD5))
            {
                throw new NullReferenceException("install path not set");
            }

6.下载更新进度通知事件,将会提供剩余下载时间(Remaining)和下载速度(Speed)

快速启动

代码语言:javascript
复制
           #region Launch1

            args = new string[6] {
                "0.0.0.0",
                "1.1.1.1",
                "https://github.com/WELL-E",
                 "http://192.168.50.225:7000/update.zip",
                 @"E:\PlatformPath",
                "509f0ede227de4a662763a4abe3d8470",
                 };

            GeneralUpdateBootstrap bootstrap = new GeneralUpdateBootstrap();
            bootstrap.DownloadStatistics += OnDownloadStatistics;
            bootstrap.ProgressChanged += OnProgressChanged;
            bootstrap.Strategy<DefultStrategy>().
                Option(UpdateOption.Format, "zip").//指定更新包的格式,目前只支持zip
                Option(UpdateOption.MainApp, "your application name").//指定更新完成后需要启动的主程序名称不需要加.exe直接写名称即可
                RemoteAddress(args).//这里的参数保留了之前的参数数组集合
                Launch();

            #endregion

            #region Launch2

            /*
             * Launch2
             * 新增了第二种启动方式
             * 流程:
             * 1.指定更新地址,https://api.com/GeneralUpdate?version=1.0.0.1 在webapi中传入客户端当前版本号
             * 2.如果需要更新api返回给你所有的更新信息(详情内容参考 /Models/UpdateInfo.cs)
             * 3.拿到更新信息之后则开始http请求更新包
             * 4.下载
             * 5.解压
             * 6.更新本地文件
             * 7.关闭更新程序
             * 8.启动配置好主程序
             * 更新程序必须跟主程序放在同级目录下
             */

            //GeneralUpdateBootstrap bootstrap2 = new GeneralUpdateBootstrap();
            //bootstrap2.DownloadStatistics += OnDownloadStatistics;
            //bootstrap2.ProgressChanged += OnProgressChanged;
            //bootstrap2.Strategy<DefultStrategy>().
            //    Option(UpdateOption.Format, "zip").
            //    Option(UpdateOption.MainApp, "KGS.CPP").
            //    RemoteAddress(@"https://api.com/GeneralUpdate?version=1.0.0.1").//指定更新地址
            //    Launch();

            #endregion


  private static void OnProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.Type == ProgressType.Updatefile)
            {
                var str = $"当前更新第:{e.ProgressValue}个,更新文件总数:{e.TotalSize}";
                Console.WriteLine(str);
            }

            if (e.Type == ProgressType.Done)
            {
                Console.WriteLine("更新完成");
            }
        }

        private static void OnDownloadStatistics(object sender, DownloadStatisticsEventArgs e)
        {
            Console.WriteLine($"下载速度:{e.Speed},剩余时间:{e.Remaining.Minute}:{e.Remaining.Second}");
        }

开发作者:JusterZhu & WELL-E

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-05-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 JusterZhu 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 描述:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档