记录WinFrom 使用 Autoupdater.NET.Official 进行软件升级更新

开源地址:https://github.com/ravibpatel/AutoUpdater.NET

使用Autoupdater.NET.Official 更新软件版本 关键代码 (此升级关键代码从互联网上获取到,进行调整后变成如下:)

复制代码
 public static void Start(string serverPath, ISynchronizeInvoke synchronizeInvoke)
 {
     AutoUpdater.Start(serverPath, Assembly.GetExecutingAssembly());

     #region 

     System.Timers.Timer timer = new System.Timers.Timer
     {
         Interval = 2 * 60 * 1000,
         SynchronizingObject = synchronizeInvoke
     };
     timer.Elapsed += delegate
     {
         AutoUpdater.Start(serverPath, Assembly.GetExecutingAssembly());
     };
     timer.Start();

     #endregion

     //若您不想在更新表单上显示"跳过"按钮,那个么只需在上面的代码中添加以下行即可。
     AutoUpdater.ShowSkipButton = false;

     //如果要同步检查更新,请在启动更新之前将Synchronous设置为true,如下所示。
     AutoUpdater.Synchronous = true;

     //若你们不想在更新表单上显示"以后提醒"按钮,那个么只需在上面的代码中添加以下一行即可。
     AutoUpdater.ShowRemindLaterButton = false;

     //如果要忽略先前设置的"以后提醒"和"跳过"设置,则可以将"强制"属性设置为true。它还将隐藏"跳过"和"稍后提醒"按钮。如果在代码中将强制设置为true,那么XML文件中的强制值将被忽略。
     AutoUpdater.Mandatory = false;

     //您可以通过添加以下代码来打开错误报告。如果执行此自动更新程序。NET将显示错误消息,如果没有可用的更新或无法从web服务器获取XML文件。
     AutoUpdater.ReportErrors = true;

     //如果服务器xml文件的版本大于AssemblyInfo中的版本则触发CheckForUpdateEvent
     AutoUpdater.CheckForUpdateEvent += (args) => {
         if (args.Error == null)
         {
             //检测到有可用的更新
             if (args.IsUpdateAvailable)
             {
                 DialogResult dialogResult;
                 if (args.Mandatory.Value)
                 {
                     dialogResult =
                         MessageBox.Show(
                             $@"发现新版本V{args.CurrentVersion}.当前版本V{args.InstalledVersion}.点击确认开始更新", @"新版更新",
                             MessageBoxButtons.OK,
                             MessageBoxIcon.Information);
                 }
                 else
                 {
                     dialogResult =
                         MessageBox.Show(
                             $@"发现新版本V{args.CurrentVersion}.当前版本V{args.InstalledVersion}.确认要更新吗?", @"新版更新",
                             MessageBoxButtons.YesNo,
                             MessageBoxIcon.Information);
                 }

                 if (dialogResult.Equals(DialogResult.Yes) || dialogResult.Equals(DialogResult.OK))
                 {
                     try
                     {
                         //触发更新下载
                         if (AutoUpdater.DownloadUpdate(args))
                         {
                             Application.Exit();
                         }
                     }
                     catch (Exception exception)
                     {
                         MessageBox.Show(exception.Message, exception.GetType().ToString(), MessageBoxButtons.OK,
                             MessageBoxIcon.Error);
                     }
                 }
             }
         }
         else
         {
             if (args.Error is WebException)
             {
                 MessageBox.Show(
                     @"连接服务器失败,请检查网络连接.",
                     @"更新检查失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
             else
             {
                 MessageBox.Show(args.Error.Message,
                     args.Error.GetType().ToString(), MessageBoxButtons.OK,
                     MessageBoxIcon.Error);
             }
         }
     };
 }

配置升级xml,创建AutoUpdater.xml 文件,添加以下配置

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<item>
    <version>1.0.0.1</version>
    <url>http://localhost:8085/BatchCopyData.exe</url>
    <changelog>http://localhost:8085/AutoUpdaterLoger.html</changelog>
    <mandatory>false</mandatory>
</item>
相关推荐
q***49862 分钟前
Spring Boot 3.4 正式发布,结构化日志!
java·spring boot·后端
西洼工作室1 小时前
项目环境变量配置全攻略
前端
阿珊和她的猫1 小时前
Webpack 优化:构建速度与包体积的双重提升
前端·webpack·node.js
阿珊和她的猫1 小时前
Webpack 打包体积优化:让应用更轻量、更高效
前端·webpack·状态模式
im_AMBER1 小时前
Vite + React 项目启动深度踩坑指南
前端·学习·react.js·前端框架
Hammer Ray2 小时前
前端开发基础概念(React)
前端·react.js·前端框架
沐浴露z3 小时前
【微服务】基本概念介绍
java·微服务
Z3r4y3 小时前
【代码审计】RuoYi-4.7.3&4.7.8 定时任务RCE 漏洞分析
java·web安全·ruoyi·代码审计
请叫我阿杰3 小时前
Ubuntu系统安装.NET SDK 7.0
数据库·ubuntu·.net
Sunlightʊə4 小时前
2.登录页测试用例
运维·服务器·前端·功能测试·单元测试