WinForm、Wpf自动升级 AutoUpdater.NET(AutoUpdaterDotNet)

本文介绍了如何使用AutoUpdater.NET在IIS环境下部署Winform应用,包括创建IIS部署更新站点、配置XML文件以指定更新信息,并在Winform中集成自动更新功能,以及打包和手动/自动更新过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Github

AutoUpdater.NET

目录

一、IIS部署 更新站点

二、创建Winform


一、IIS部署 更新站点

IIS默认站点目录下创建 目录 Downloads、Updates

Updates目录创建文件 UpdateLog.html、AutoUpdaterStarter.xml

UpdateLog.html:

<html>
    <body>
        <h1>
            UpDate
        </h1>
    </body>

</html>

 AutoUpdaterStarter.xml:

url节点 为下载更新的地址 http://127.0.0.1/Downloads/fr.zip

<?xml version='1.0' encoding="UTF-8"?>
<item>
    <!--在版本标记之间提供应用程序的最新版本。版本必须为X.X.X.X格式。-->
    <version>1.0.0.2</version>
 
    <!--在url标签之间提供最新版本安装程序文件或zip文件的url。自动更新。NET下载这里提供的文件,并在用户按下Update按钮时安装它。-->
    <url>http://127.0.0.1/Downloads/fr.zip</url>
 
    <!--在changelog标记之间提供应用程序更改日志的URL。如果你不提供变更日志的URL,那么更新对话框将不会显示变更日志。-->
    <changelog>http://127.0.0.1/Updates/UpdateLog.html</changelog>
 
    <!--如果你不想让用户跳过这个版本,可以将其设置为true。这将忽略“稍后提醒”和“跳过”选项,并在更新对话框中隐藏“稍后提醒”和“跳过”按钮。-->
    <!--mandatory mode="2">true</mandatory -->
 
    <!--可以在强制元素上提供minVersion属性。当您提供该选项时,只有当安装的应用程序版本小于您在这里指定的最小版本时才会触发强制选项。-->
    <!--mandatory minVersion="1.2.0.0">true</mandatory -->
 
    <!--args(可选):您可以在这个标记之间为安装程序提供命令行参数。您可以在命令行参数中包含%path%,它将被当前正在执行的应用程序所在目录的path所取代。-->
    <!--mandatory args="xxxxx">false</mandatory -->
 
    <!--提供更新文件的校验和。如果你做这个autotoupater。NET将在执行更新过程之前比较下载文件的校验和,以检查文件的完整性。
    您可以在校验和标记中提供algorithm属性,以指定应该使用哪个算法来生成下载文件的校验和。目前支持MD5、SHA1、SHA256、SHA384和SHA512。-->
    <!--checksum algorithm="MD5">Update file Checksum</checksum -->
</item>

二、创建Winform

netcore 3.1 + Winform 

nuget安装包

Autoupdater.NET.Official        --版本1.8.0

 2.1创建MainForm

http://127.0.0.1/Updates/AutoUpdaterStarter.xml 为IIS站点更新配置文件

检查版本

    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            Assembly assembly = Assembly.GetEntryAssembly();
            label1.Text = $"{assembly.GetName().Version}";//显示版本号

            AutoUpdatorHelper.Start("http://127.0.0.1/Updates/AutoUpdaterStarter.xml", this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AutoUpdater.Start("http://127.0.0.1/Updates/AutoUpdaterStarter.xml");//手动更新
        }

        public class AutoUpdatorHelper
        {
            /// <summary>
            /// 自动更新
            /// </summary>
            /// <param name="serverPath"></param>
            /// <param name="synchronizeInvoke"></param>
            public static void Start(string serverPath, ISynchronizeInvoke synchronizeInvoke)
            {
                #region 每隔60秒检查一次更新(判断依据是AssemblyInfo中的版本和xml文件的版本是否一致,如果服务器xml文件的版本大于AssemblyInfo中的版本则触发CheckForUpdateEvent)
                System.Timers.Timer timer = new System.Timers.Timer
                {
                    Interval = 60 * 1000,//毫秒
                    SynchronizingObject = synchronizeInvoke
                };
                timer.Elapsed += (object sender, ElapsedEventArgs e) =>
                {
                    AutoUpdater.Start(serverPath, Assembly.GetExecutingAssembly());
                };
                timer.Start();
                #endregion

                AutoUpdater.LetUserSelectRemindLater = true;
                AutoUpdater.RemindLaterTimeSpan = RemindLaterFormat.Minutes;
                AutoUpdater.RemindLaterAt = 1;

                //若您不想在更新表单上显示“跳过”按钮,那个么只需在上面的代码中添加以下行即可。
                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(
                                        $@"当前有一个新版本{args.CurrentVersion}可用.你正在使用版本{args.InstalledVersion}.点击确认开始更新", @"更新可用",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                            }
                            else
                            {
                                dialogResult =
                                    MessageBox.Show(
                                        $@"当前有一个新版本{args.CurrentVersion}可用.你正在使用版本{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
                        {
                            MessageBox.Show(
                                $@"当前为最新新版本", @"更新可用",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                        }
                    }
                    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);
                        }
                    }
                };
            }
        }
    }

 2.2打包

winfrom生成文件添加到压缩文件 fr.zip,复制到IIS站点Downloads目录下

2.3更新

 手动更新

 自动更新从版本1.0.0.1 更新到1.0.0.2 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值