AutoUpdater.NET 客戶端更新指定文件,具體使用如下
1、新建項目windows窗體項目--WindowsFormTestUpdate
2、添加NuGet程序管理包
選擇項目右鍵,選擇“管理NuGet程序包”,搜索“Autoupdater.NET.Official”,選擇,點擊安裝
3、窗體代碼如下:
界面
代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using AutoUpdaterDotNET; using Newtonsoft.Json; namespace WindowsFormTestUpdate { public partial class TestUpdateForm : Form { public TestUpdateForm() { InitializeComponent(); } private void TestUpdateForm_Load(object sender, EventArgs e) { lblVersion.Text = "版本:" + Application.ProductName + Application.ProductVersion; } private void btnAutoUpdate_Click(object sender, EventArgs e) { AutoUpdater.ReportErrors = true; //1、根據服務端的更新文件update.xml的version檢測是否與當前應用程序版本一致,是否需要更新。 //AutoUpdater.Start("http://localhost:8082/TestAutoUpdate/update.xml"); //2、根據服務端的更新文件update.xml的version檢測是否與指定myAssembly應用程序版本一致,是否需要更新。 //AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.xml", System.Reflection.Assembly.GetExecutingAssembly()/*this.GetType().Assembly*/); //3、根據FTP服務端的更新文件update.xml的version檢測是否與指定myAssembly應用程序版本一致,是否需要更新。 //AutoUpdater.Start("ftp://127.0.0.1/TestAutoUpdate/update.xml", new NetworkCredential("myuser", "mypassword")); //其他屬性設置 //設置更新彈窗的大小 //AutoUpdater.UpdateFormSize = new System.Drawing.Size(800, 600); ////AutoUpdater.AppCastURL = "";//服務端的更新文件update.xml ////AutoUpdater.InstalledVersion = new Version("1.3"); //AutoUpdater.AppTitle = "更新彈窗";//彈出窗體標題 ////AutoUpdater.DownloadPath = "";//升級下載包路徑 ////AutoUpdater.PersistenceProvider = "";//升級下載包路徑 ////AutoUpdater.Mandatory = true; ////AutoUpdater.Mandatory = true; ////AutoUpdater.UpdateMode = Mode.Forced; //AutoUpdater.ShowRemindLaterButton = true;//顯示稍后提醒按鈕 ////AutoUpdater.LetUserSelectRemindLater = false; ////AutoUpdater.RemindLaterTimeSpan = RemindLaterFormat.Days; ////AutoUpdater.RemindLaterAt = 2; //AutoUpdater.ShowSkipButton = true;//顯示稍后提醒按鈕 //AutoUpdater.Synchronous = true;//顯示稍后提醒按鈕 ////var proxy = new WebProxy("ProxyIP:ProxyPort", true) ////{ //// Credentials = new NetworkCredential("ProxyUserName", "ProxyPassword") ////}; ////AutoUpdater.Proxy = proxy; ////設置憑證 ////BasicAuthentication basicAuthentication = new BasicAuthentication("myUserName", "myPassword"); ////AutoUpdater.BasicAuthXML = AutoUpdater.BasicAuthDownload = AutoUpdater.BasicAuthChangeLog = basicAuthentication; ////瀏覽器打開下載 ////AutoUpdater.OpenDownloadPage = true; //string jsonPath = Path.Combine(Environment.CurrentDirectory, "settings.json"); //AutoUpdater.PersistenceProvider = new JsonFilePersistenceProvider(jsonPath); ////Handling Application exit logic manually,自定義更新成功之后操作 //AutoUpdater.ApplicationExitEvent += AutoUpdater_ApplicationExitEvent; //自定義更新 //AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent; //自定義更新文件轉換,可以指定json類型的文件,如下 //AutoUpdater.ParseUpdateInfoEvent += AutoUpdaterOnParseUpdateInfoEvent; //AutoUpdater.Start("http://localhost:8082/AutoUpdaterTest.json"); #region 指定json類型的文件 // { // "version":"2.0.0.0", //"url":"http://rbsoft.org/downloads/AutoUpdaterTest.zip", //"changelog":"https://github.com/ravibpatel/AutoUpdater.NET/releases", //"mandatory":{ // "value":true, // "minVersion": "2.0.0.0", // "mode":1 //}, //"checksum":{ // "value":"E5F59E50FC91A9E52634FFCB11F32BD37FE0E2F1", // "hashingAlgorithm":"SHA1" //} // } #endregion //Text = Application.StartupPath; AutoUpdater.Start("http://localhost:8082/TestAutoUpdate/update.xml"); } private void AutoUpdater_ApplicationExitEvent() { //Text = @"Closing application..."; ////Thread.Sleep(5000); //Application.Exit(); //string exepath = @"C:\Users\Administrator\Desktop\Debug\AutoUpdaterTest"; //var currentDirectory = new DirectoryInfo(Application.StartupPath); //if (currentDirectory.Parent != null) //{ // exepath = Path.Combine(currentDirectory.Parent.FullName, "TestAutoUpdate", "WindowsFormTestUpdate.exe"); //} //lblVersion.Text = lblVersion.Text + $"----{exepath}"; //System.Diagnostics.Process.Start(exepath); } private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args) { if (args.Error == null) { if (args.IsUpdateAvailable) { DialogResult dialogResult; if (args.Mandatory.Value) { dialogResult = MessageBox.Show( $@"There is new version {args.CurrentVersion} available. You are using version {args.InstalledVersion}. This is required update. Press Ok to begin updating the application.", @"Update Available", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { dialogResult = MessageBox.Show( $@"There is new version {args.CurrentVersion} available. You are using version { args.InstalledVersion }. Do you want to update the application now?", @"Update Available", MessageBoxButtons.YesNo, MessageBoxIcon.Information); } // Uncomment the following line if you want to show standard update dialog instead. // AutoUpdater.ShowUpdateForm(args); 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(@"There is no update available please try again later.", @"No update available", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { if (args.Error is WebException) { MessageBox.Show( @"There is a problem reaching update server. Please check your internet connection and try again later.", @"Update Check Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show(args.Error.Message, args.Error.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void AutoUpdaterOnParseUpdateInfoEvent(ParseUpdateInfoEventArgs args) { dynamic json = JsonConvert.DeserializeObject(args.RemoteData); args.UpdateInfo = new UpdateInfoEventArgs { CurrentVersion = json.version, ChangelogURL = json.changelog, DownloadURL = json.url, Mandatory = new Mandatory { Value = json.mandatory.value, UpdateMode = json.mandatory.mode, MinimumVersion = json.mandatory.minVersion }, CheckSum = new CheckSum { Value = json.checksum.value, HashingAlgorithm = json.checksum.hashingAlgorithm } }; } } }
注意:
1、 獲取配置文件方式可以是網站服務器,也可以是ftp服務器,配置文件可以是xml的,當然也可以是其他格式的,但是其他格式的,如json,獲取方式就不一樣了(AutoUpdater.Start("http://localhost:8082/AutoUpdaterTest.json"))
一般是使用如下:
//1、根據服務端的更新文件update.xml的version檢測是否與當前應用程序版本一致,是否需要更新。
AutoUpdater.Start("http://localhost:8082/TestAutoUpdate/update.xml");
//2、根據服務端的更新文件update.xml的version檢測是否與指定myAssembly應用程序版本一致,是否需要更新。
AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.xml", System.Reflection.Assembly.GetExecutingAssembly()/*this.GetType().Assembly*/);
//3、根據FTP服務端的更新文件update.xml的version檢測是否與指定myAssembly應用程序版本一致,是否需要更新。
AutoUpdater.Start("ftp://127.0.0.1/TestAutoUpdate/update.xml", new NetworkCredential("myuser", "mypassword"));
2、程序集版本(不同版本的程序集可以通過程序集版本來控制,以示區別)
4、運行結果
5、其他的一些配置可以參考:
https://github.com/ravibpatel/AutoUpdater.NET