AutoUpdater.NET 是一款用於WPF、Winform軟件版本更新的框架,類似框架還有Squirrel、WinSparkle、NetSparkle、Google Omaha。
一、Web端更新服務器配置
我們以本地IIS服務器為例介紹:(實際可以使用公網的服務器搭建網站進行更新)
勾選上訴功能完成IIS服務器安裝。
在默認Web網站中新建兩個文件夾,Downloads用於存放更新的文件(.zip的壓縮文件),Updates用於存放更新的配置文件和更新的日志文件。
二、新建更新配置文件和日志文件 AutoUpdaterStarter.xml和UpdateLog.html
<?xml version='1.0' encoding="UTF-8"?> <item> <!--在版本標記之間提供應用程序的最新版本。版本必須為X.X.X.X格式。--> <version>1.0.0.1</version> <!--在url標簽之間提供最新版本安裝程序文件或zip文件的url。自動更新。NET下載這里提供的文件,並在用戶按下Update按鈕時安裝它。--> <url>http://192.168.1.40/Downloads/Update.zip</url> <!--在changelog標記之間提供應用程序更改日志的URL。如果你不提供變更日志的URL,那么更新對話框將不會顯示變更日志。--> <changelog>http://192.168.1.40/Updates/UpdateLog.html</changelog> <!--如果你不想讓用戶跳過這個版本,可以將其設置為true。這將忽略“稍后提醒”和“跳過”選項,並在更新對話框中隱藏“稍后提醒”和“跳過”按鈕。--> <mandatory>false</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>
<html> <body> <h1> UpDate </h1> </body> </html>
三、WPF軟件更新配置
你的應用程序安裝Autoupdater.NET.Official Nuget包。https://github.com/ravibpatel/AutoUpdater.NET
在初始化的位置配置更新的配置:
using System; using System.Globalization; using System.Reflection; using System.Threading; using System.Windows; using System.Windows.Threading; using AutoUpdaterDotNET; namespace AutoUpdaterTestWPF { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Assembly assembly = Assembly.GetEntryAssembly(); LabelVersion.Content = $"Current Version : {assembly.GetName().Version}";//顯示版本號 Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("zh"); AutoUpdater.LetUserSelectRemindLater = true; AutoUpdater.RemindLaterTimeSpan = RemindLaterFormat.Minutes; AutoUpdater.RemindLaterAt = 1; AutoUpdater.ReportErrors = true; DispatcherTimer timer = new DispatcherTimer {Interval = TimeSpan.FromMinutes(2)};//定時去檢測更新根據自己業務需求 timer.Tick += delegate { AutoUpdater.Start("http://192.168.1.40/Updates/AutoUpdaterStarter.xml"); }; timer.Start(); } private void ButtonCheckForUpdate_Click(object sender, RoutedEventArgs e) { AutoUpdater.Start("http://192.168.1.40/Updates/AutoUpdaterStarter.xml"); } } }
這里簡單介紹幾個更新有用的配置:
//通過將其分配給InstalledVersion字段來提供自己的版本
AutoUpdater.InstalledVersion = new Version("1.2");
//查看中文版本
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture("zh");
//顯示自定義的應用程序標題
AutoUpdater.AppTitle ="升級更新";
//不顯示“稍后提醒”按鈕
AutoUpdater.ShowRemindLaterButton = false;
//強制選項將隱藏稍后提醒,跳過和關閉按鈕的標准更新對話框。
AutoUpdater.Mandatory = true;
AutoUpdater.UpdateMode = Mode.Forced;
//為XML、更新文件和更改日志提供基本身份驗證
BasicAuthentication basicAuthentication = new BasicAuthentication("myUserName", "myPassword");
AutoUpdater.BasicAuthXML = AutoUpdater.BasicAuthDownload = AutoUpdater.BasicAuthChangeLog = basicAuthentication;
//為http web請求設置User-Agent
AutoUpdater.HttpUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36";
//啟用錯誤報告
AutoUpdater.ReportErrors = true;
//將應用程序設定不需要管理員權限來替換舊版本
AutoUpdater.RunUpdateAsAdmin = false;
//打開下載頁面,不直接下載最新版本*****
AutoUpdater.OpenDownloadPage = true;
//設置為要下載更新文件的文件夾路徑。如果沒有提供,則默認為臨時文件夾。
//AutoUpdater.DownloadPath = Environment.CurrentDirectory;
//設置zip解壓路徑
AutoUpdater.InstallationPath = Environment.CurrentDirectory;
//處理應用程序在下載完成后如何退出
AutoUpdater.ApplicationExitEvent += AutoUpdater_ApplicationExitEvent;
//自定義處理更新邏輯事件
AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent;
在AutoUpdaterOnCheckForUpdateEvent事件中可以自己處理是否更新,在自定義的升級程序很有用處,可以自己定義升級的界面等。
四、更新包的打包
AutoUpdater.NET通過檢測應用程序的版本號來判斷需不需要更新軟件,這里我們需要操作
軟件更新后設置這里軟件版本號,同時更新AutoUpdaterStarter.xml中的版本信息,UpdateLog.html里面的日志文件,然后將軟件的Release版本打包為.zip放入Downloads文件夾中。
用戶在運行舊版軟件就會檢測到新版的更新,然后選擇是否進行更新。
注:測試時UpdateLog.html中使用中文會顯示亂碼,原因應該時這里嵌入的是IE內核的瀏覽器所以需要注意兼容IE瀏覽器。