0
点赞
收藏
分享

微信扫一扫

WinForm、Wpf自动升级 AutoUpdater.NET

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安装包

 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 =
    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);
                        }
                    }
                };
            }
        }
    }
quot;{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 

举报

相关推荐

0 条评论