记录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>
相关推荐
Resean02234 小时前
架构设计系列(十六):DDD 与代码架构
java·开发语言·架构
学长毕设5 小时前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
feixing_fx6 小时前
解构与展开:可读性和性能的取舍
开发语言·前端·javascript·前端框架
Flynt7 小时前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
前端架构师-老李7 小时前
html教程:header、nav、main、footer 四个标签各管哪一块?
前端·html
烟雨空灵8 小时前
25 天做了个独立开发者作品橱窗,收录 340+ 件作品,代码全开源
前端·node.js
wangruofeng8 小时前
怎么让你的项目更好看?用 UI Lab 练设计原则,给界面去 AI 味
前端·aigc·ai编程
用户69371750013849 小时前
AI 越强,为什么我们反而越来越担心失业?
前端·后端·github
她的男孩10 小时前
Spring Boot 接 Flowable:请假单要同时走财务和行政,包容分支和默认分支怎么配
java·spring boot·工作流引擎
涛涛ing10 小时前
线上突发大面积白屏,排查一天后,我发现是 `JSON.parse` 背了锅
前端