记录WinFrom 使用 Autoupdater.NET.Official 进行软件升级更新,避免遗忘
此升级关键代码从互联网上获取到,进行调整后变成如下:
使用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中的版本则触发CheckForUpdateEventAutoUpdater.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>