软件自动升级程序AutoUpgrader优化:获取最新升级包|C/S开发框架
作者:csframework|C/S框架网  发布日期:2022/01/21 10:34:11

软件自动升级程序AutoUpgrader优化:获取最新升级包|C/S开发框架

软件自动升级程序CSFramework.AutoUpgrader优化完成,CheckVersion方法由原来的临时文件方式更改为通过.NET反射机制调用DLL的方法。

解决方案

UpgraderController.cs 新增一个函数

VS打开 CSFramework.AutoUpgrader 解决方案,在UpgraderController.cs 新增一个函数:

C# 全选
 /// <summary>
        /// 检查是否有新的升级包
        /// </summary>
        /// <returns>返回升级包数量</returns>
        public int GetNewlyPackageCount()
        {
            UpgraderConfig.ReadConfig();//读取本地配置            
            IDownloader download = CreateDownloader();//创建升级策略
            if (download.TestConnection())//测试连接数据库
            {
                IList files = download.GetServerPackageFile();//获取最新的升级包

                var log = "检测可下载升级包数:" + files.Count.ToString();
                UpgraderLog.AddLog(log);
                //UpgraderLogDB.AddLog("CheckPkg", log);

                return files.Count;
            }
            else
            {
                UpgraderLog.AddLog("连接版本升级服务器失败!");
                throw new Exception("连接版本升级服务器失败!");
            }
        }

软件自动升级程序AutoUpgrader优化:获取最新升级包|C/S开发框架

VersionCheckNEW.cs 替换CheckVersion方法的代码

VS打开主程序,找到VersionCheckNEW.cs文件,替换CheckVersion方法

C# 全选
 /// <summary>
        /// 检查更新,有更新要退出主程序
        /// </summary>
        /// <param name="AppExit">退出应用程序</param>
        public static void CheckVersion(ref bool AppExit)
        {
            AppExit = false;
            try
            {
                string upgraderPath =Path.Combine(Application.StartupPath,DEF_UPGRADER_NAME);

                //获取升级程序的程序集
                byte[] fileData = File.ReadAllBytes(upgraderPath);
                Assembly upgrader = Assembly.Load(fileData);

                //通过.NET反射机制创建UpgraderController实例
                Type t = upgrader.GetType(DEF_UPGRADER_CONTROLLER);
                object o = t.Assembly.CreateInstance(t.FullName);

                //获取升级包数量
                MethodInfo M = t.GetMethod("GetNewlyPackageCount");
                object ret = M.Invoke(o, null);
                int count = ConvertEx.ToInt(ret);

                frmWaitingEx.HideMe(null);

                if (count > 0)//有新版本,升级包的文件数>0
                {
                    AppExit = true;

                    //启动通用升级程序(商业版)。程序参数:MainEXECall YourERP.exe
                    Process.Start(upgraderPath, Arg_MainEXECall + " " + DEF_MAIN_PROGRAM_NAME);
                }
            }
            catch (Exception ex)
            {
                dalLog.AddLog(ex);
                Msg.ShowError("检查版本失败!CheckVersion:" + ex.Message);
            }
        }

软件自动升级程序AutoUpgrader优化:获取最新升级包|C/S开发框架

C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务
上一篇 下一篇