记录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>
相关推荐
老毛肚17 小时前
MyBatis体系结构与工作原理 上篇
java·mybatis
风流倜傥唐伯虎17 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
不像程序员的程序媛18 小时前
Nginx日志切分
服务器·前端·nginx
Yvonne爱编码18 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚18 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
北原_春希18 小时前
如何在Vue3项目中引入并使用Echarts图表
前端·javascript·echarts
你这个代码我看不懂18 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
尽意啊18 小时前
echarts树图动态添加子节点
前端·javascript·echarts
吃面必吃蒜18 小时前
echarts 极坐标柱状图 如何定义柱子颜色
前端·javascript·echarts
O_oStayPositive18 小时前
Vue3使用ECharts
前端·javascript·echarts