關於AutoUpdater.Net應用與實現


前言

AutoUpdaterDotNet是Ravi Patel大神的力作,來源 https://github.com/ravibpatel/AutoUpdater.NET 。完全支持遠程服務器版本檢查及在線下載更新版本,客戶端更兼容IE/Chrome/FireFox等眾多瀏覽器軟件。本着研究看的心態,同時看了許多關於AutoUpdaterDotNet控件技術的文章,感覺缺少了許多東西,特留下此篇文檔供新手參考使用。如果有什么問題之處,敬請指正。

一、環境搭建

1、下載及應用方式:

可以在VS中通過:

PM> Install-Package Autoupdater.NET.Official

  直接進行加載進項目中。

也可以,在github下載zip,通過dll庫進行引用。

 

2、Web服務搭建

可以應用於IIS環境,也可以應用在Apache/easyhttp/GuaikNet等眾多Web服務端環境。只需要確認環境完全遵行html5 + css3 + js就可以。此篇文章以搭載IIS環境來展開:

a. 搭建好IIS后,在IIS管理器中Default Web Site網站下先配置好,兩個子目錄:

 

 

 Downloads是存放軟件升級所需要的補丁包(.zip格式),Updates是存放AutoUpdaterStarter.xml環境配置文件(.xml)以及更新完成后呈現的更新日志Page(UpdateLog.html)。

AutoUpdaterStarter.xml源碼:(其中標紅字的為必設定項,而標藍字的為可選設定項。標黑字的mandatory,需要前端UI處理args傳參值)

<?xml version='1.0' encoding="UTF-8"?>
<item>
    <!--在版本標記之間提供應用程序的最新版本。版本必須為X.X.X.X格式。-->
    <version>2.3.2.0</version>

    <!--在url標簽之間提供最新版本安裝程序文件或zip文件的url。自動更新。NET下載這里提供的文件,並在用戶按下Update按鈕時安裝它。-->
    <url>http://192.168.1.8/Downloads/GasMisUpdateHotFix.zip</url>

    <!--在changelog標記之間提供應用程序更改日志的URL。如果你不提供變更日志的URL,那么更新對話框將不會顯示變更日志。-->
    <changelog>http://192.168.1.8/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>

  更新日志UpdateLog.html頁面的設計,可隨意自行設計。完全符合並支持html5 + css3 + js代碼標准。需要強調的是,IIS如果開啟並支持了https協議,開啟了SSL簽名認證(IIS對目錄啟用了SSL設置),沒有關系直接在xml源碼中將http://改成https://就可以了,但開啟了基本身份驗證,就需UI前端設定相關參數(請見下文)

b. 前端UI設計

有兩種加載方式:

(1) 通過建立一個CheckforUpdate的窗體,將AutoUpdater.Start方法寫入本窗體CheckforUpdate_Load事件中;最后在項目啟動類中FormLoad()事件加載並實例化該窗體。

(2)直接在項目啟動類中調用AutoUpdater.Start方法,簡單直觀。

demo如下:

  1 namespace CusPro.GasMis.Starter
  2 {
  3     public partial class FrmLogin : Form
  4     {
  5         public FrmLogin()
  6         {
  7             InitializeComponent();
  8         }
  9 
 10         private void FrmLogin_Load(object sender, EventArgs e)
 11         {
 12             #region 更新提示/判斷是否自動更新
 13             AutoUpdaterStarter();
 14             #endregion
 15 
 16         }
 17 
 18         private void AutoUpdaterStarter()
 19         {
 20             //XML文件服務器下載地址
 21             
 22        AutoUpdater.Start("http://192.168.1.8/Updates/AutoUpdaterStarter.xml");
 23 
 24             //通過將其分配給InstalledVersion字段來提供自己的版本
 25             //AutoUpdater.InstalledVersion = new Version("1.2");
 26 
 27             //查看中文版本
 28             //Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture("zh");
 29 
 30             //顯示自定義的應用程序標題
 31             AutoUpdater.AppTitle = config.AppName + "升級更新";
 32 
 33             //不顯示“稍后提醒”按鈕
 34             //AutoUpdater.ShowRemindLaterButton = false;
 35 
 36             //強制選項將隱藏稍后提醒,跳過和關閉按鈕的標准更新對話框。
 37             //AutoUpdater.Mandatory = true;
 38             //AutoUpdater.UpdateMode = Mode.Forced;
 39 
 40             //為XML、更新文件和更改日志提供基本身份驗證  41             //BasicAuthentication basicAuthentication = new BasicAuthentication("myUserName", "myPassword");
 42             //AutoUpdater.BasicAuthXML = AutoUpdater.BasicAuthDownload = AutoUpdater.BasicAuthChangeLog = basicAuthentication;
 43 
 44             //為http web請求設置User-Agent
 45             //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";
 46 
 47             //啟用錯誤報告
 48             AutoUpdater.ReportErrors = true;
 49 
 50             //將應用程序設定不需要管理員權限來替換舊版本
 51             //AutoUpdater.RunUpdateAsAdmin = false;
 52 
 53             //打開下載頁面,不直接下載最新版本*****
 54             AutoUpdater.OpenDownloadPage = true;
 55 
 56             //設置為要下載更新文件的文件夾路徑。如果沒有提供,則默認為臨時文件夾。
 57             //AutoUpdater.DownloadPath = Environment.CurrentDirectory;
 58 
 59             //設置zip解壓路徑
 60             AutoUpdater.InstallationPath = Environment.CurrentDirectory;
 61 
 62             //處理應用程序在下載完成后如何退出
 63             AutoUpdater.ApplicationExitEvent += AutoUpdater_ApplicationExitEvent;
 64 
 65             //自定義處理更新邏輯事件
 66             AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent;
 67         }
 68 
 69 
 70         private void AutoUpdater_ApplicationExitEvent()
 71         {
 72             Text = @"Closing application...";
 73             Thread.Sleep(5000);
 74             Application.Exit();
 75         }
 76 
 77         private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args)
 78         {
 79             if (args.Error == null)
 80             {
 81                 if (args.IsUpdateAvailable)
 82                 {
 83                     DialogResult dialogResult;
 84                     if (args.Mandatory.Value)
 85                     {
 86                         dialogResult =
 87                             MessageBox.Show(
 88                                 $@"您有新的版本 {args.CurrentVersion} 可用,當前使用的是{
 89                                         args.InstalledVersion
 90                                     } 版本,這是必需的更新。",
 91                                 @"Update Available",
 92                                 MessageBoxButtons.OK,
 93                                 MessageBoxIcon.Information);
 94                     }
 95                     else
 96                     {
 97                         dialogResult =
 98                             MessageBox.Show(
 99                                 $@"您有新的版本 {args.CurrentVersion} 可用,當前使用的是{args.InstalledVersion} 版本,
100                                 您現在要更新應用程序嗎?", @"Update Available",
101                                 MessageBoxButtons.YesNo,
102                                 MessageBoxIcon.Information);
103                     }
104 
105 
106                     if (dialogResult.Equals(DialogResult.Yes) || dialogResult.Equals(DialogResult.OK))
107                     {
108                         try
109                         {
110                             //You can use Download Update dialog used by AutoUpdater.NET to download the update.
111 
112                             if (AutoUpdater.DownloadUpdate(args))
113                             {
114                                 Application.Exit();
115                             }
116                         }
117                         catch (Exception exception)
118                         {
119                             MessageBox.Show(exception.Message, exception.GetType().ToString(), MessageBoxButtons.OK,
120                                 MessageBoxIcon.Error);
121                         }
122                     }
123                 }
124                 else
125                 {
126                     MessageBox.Show(@"There is no update available. Please try again later.", @"Update Unavailable",
127                         MessageBoxButtons.OK, MessageBoxIcon.Information);
128                 }
129             }
130             else
131             {
132                 if (args.Error is System.Net.WebException)
133                 {
134                     MessageBox.Show(
135                         @"There is a problem reaching update server. Please check your internet connection and try again later.",
136                         @"Update Check Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
137                 }
138                 else
139                 {
140                     MessageBox.Show(args.Error.Message,
141                         args.Error.GetType().ToString(), MessageBoxButtons.OK,
142                         MessageBoxIcon.Error);
143                 }
144             }
145         }
146     }
147 
148 }

 

二、功能性測試

運行測試的結果:

值得注意的是,我采用的是項目程序集的版本號與xml中的版本號進行比對檢查,只要是小於xml文件版本號,即會認為需要升級更新。

而升級的邏輯處理,可以通過xml與AutoUpdater相關參數:

xml參數

<mandatory mode="2">true</mandatory>

AutoUpdater參數(前端UI)

AutoUpdater.ShowRemindLaterButton = false;

<!--提供自己的程序集,那么可以通過提供Start方法的第二個參數來實現,如下所示。
AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.xml", myAssembly);-->

<!--不想在更新表單上顯示跳過按鈕,那么只需用上面的代碼添加以下一行。
AutoUpdater.ShowRemindLaterButton = false;-->

這些參數的影響來判斷執行升級更新的邏輯。

有什么問題歡迎留言指出……

-END-

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM