自動更新(自動升級)組件分享


    自從接觸安裝部署以來就有軟件升級的需求,最簡單的就是clickonce,但無法做到深入控制,本寄希望於WIX可以自己實現,但現在還沒有找到例子。然后才自己實現。 要聲明一下,這是在聖殿騎士AutoUpdater基礎上改動過來的。基於他分享的精神,我也繼續分享。我主要做了以下改動。

    1.加入客服端安裝版本和下載版本檢測。

    2.加入更新提示。

    3.做了一些異常處理,增加了接口方法。

    4.加入了皮膚。

   按照國際慣例,先上圖:

    

 

  

原理簡介

  最基本原理還是借助於聖殿騎士大神的原理,通過檢測遠程和本地的配置文件,來提示和下載,我加入了安裝版本和下載版本的檢查,來稍微區分了一下。 

 Web服務器端的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<updateFiles>
  <file path="SCADASetupWix.msi" url="http://rj-stone:82/Content/UploadFiles/SCADASetupWix.msi" lastver="1.1.1.0" size="16142" needRestar="false"></file>
</updateFiles>

Autoupdater首先會去獲取這個配置文件,看updateFiles中有無更新文件。

客服端的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Enabled>true</Enabled>
  <ServerUrl>http://rj-stone:82/Content/UploadFiles/AutoupdateService.xml</ServerUrl>
  <UpdateFileList>
    <LocalFile path="SCADASetupWix.msi" lastver="1.1.1.0" size="16142" />
  </UpdateFileList>
</Config>

然后和本地的UpdateFileList中的文件進行比對,遠程版本新就提示更新。

方法調用

 1.IAutoUpdater 接口

 public interface IAutoUpdater
    {
        /// <summary>
        /// Updates this instance.
        /// </summary>
        void Update();

        /// <summary>
        /// Infoes the update.彈出右下角提示框
        /// </summary>
        void InfoUpdate();
        /// <summary>
        /// Gets or sets the config.
        /// </summary>
        /// <value>The config.</value>
        Config Config { get; set; }
        /// <summary>
        /// Rolls the back.
        /// </summary>
        void RollBack();
        /// <summary>
        /// Checks the registry.檢查安裝版本
        /// </summary>
        /// <returns>System.String.</returns>
        string CheckRegistry();
        /// <summary>
        /// Determines whether [has new version].
        /// </summary>
        /// <returns><c>true</c> if [has new version]; otherwise, <c>false</c>.</returns>
        UpdateResultType HasNewVersion();
        /// <summary>
        /// Runs the installer.直接安裝
        /// </summary>
        void RunInstaller();
        /// <summary>
        /// Gets the loaded version.
        /// </summary>
        /// <returns>System.String.</returns>
        string GetLoadedVersion();
        /// <summary>
        /// Gets the size of the loaded.
        /// </summary>
        /// <returns>System.String.</returns>
        string GetLoadedSize();
    }

 上面的接口在AutoUpdater.cs中實現。檢測安裝版本主要是檢測注冊表,是安裝文件中決定的。也就是讀取RegistryKey和RegistryValue

 public string CheckRegistry()
        {
            string version = "0.0.0.0";
            var rk = Registry.CurrentUser;
            var softversion = rk.OpenSubKey(ConstFile.RegistryKey);
            if (softversion != null)
            {
                version = softversion.GetValue(ConstFile.RegistryValue).ToString();
            }
            return version;
        }

自動安裝也是直接去執行,bin目錄下UploadFiles文件夾中的文件

        /// <summary>
        /// 安裝已下載的版本
        /// </summary>
        public void RunInstaller()
        {
            var path = Path.Combine(GetOldPath(), "UploadFiles");
            var app = Path.Combine(path, ConstFile.ROOLBACKFILE);
            if (File.Exists(app))
            {
                RunHelper.RunInstaller(app);
            }
            else
            {
                MessageBox.Show("文件不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

 其他方法就不一一例舉了。大家可以去看源碼。

2.調用

using System;
using System.Windows.Forms;
using AutoUpdater.AutoUpdateHelper;

  
public partial class Form1 : Form { public Form1() { InitializeComponent(); IniAutoUpdater(); } private IAutoUpdater _autoUpdater; Sunisoft.IrisSkin.SkinEngine se;//皮膚 private void Form1_Load(object sender, EventArgs e) { CheckLocal(); se = new Sunisoft.IrisSkin.SkinEngine { SkinAllForm = true, SkinFile = @"..\..\skin\EmeraldColor2.ssk" }; } /// <summary> /// 讀取本地config,安裝版本信息 /// </summary> private void CheckLocal() { //名稱 AppLab.Text = ConstFile.ROOLBACKFILE; //下載版本 loadVersionLab.Text = _autoUpdater.GetLoadedVersion(); //下載大小 LengthLab.Text = _autoUpdater.GetLoadedSize(); //已安裝版本 VersionLab.Text = _autoUpdater.CheckRegistry(); } private void CheckUpdate() { var result = _autoUpdater.HasNewVersion(); if (result == UpdateResultType.Remote) { TopMost = false; update(); } if (result == UpdateResultType.Local) { var result1 = MessageBox.Show("當前下載版本已經是最新版本,是否安裝?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (result1 == DialogResult.OK) { _autoUpdater.RunInstaller(); } } if (result == UpdateResultType.None) { MessageBox.Show("安裝版本已經是最新版本", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } /// <summary> /// 初始化 /// </summary> private void IniAutoUpdater() { _autoUpdater = new AutoUpdater.AutoUpdateHelper.AutoUpdater(); } private void SCADAnotifyIcon_MouseClick(object sender, MouseEventArgs e) { if (Visible) { Hide(); } else { Show(); } } private void openToolStripMenuItem_Click(object sender, EventArgs e) { Visible = true; } private void CheckUpdateBt_Click(object sender, EventArgs e) { CheckUpdate(); } // 觸發安裝 private void UpdateBt_Click(object sender, EventArgs e) { _autoUpdater.RunInstaller(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { SCADAnotifyIcon.Visible = false; Close(); Application.Exit(); } private void updateToolStripMenuItem_Click(object sender, EventArgs e) { CheckUpdate(); } private void hideToolStripMenuItem_Click(object sender, EventArgs e) { Hide(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; Hide(); } }

 主要是checkupdate這個函數,將檢測結果分為了三類,一個是遠程有新版本,一個是最新版本已經下載,一個是當前已經是最新版本。來給出不同的提示。下面的部分是托盤里面的程序。

 

百度雲下載地址:http://pan.baidu.com/s/1c03qM4K 

 參考博客:http://www.cnblogs.com/KnightsWarrior/archive/2010/10/20/1856255.html 

 希望對你有幫助~

(弱弱的感嘆下,博客園的流量不如以前了。能像知乎、雪球、InfoQ那樣的社區就好了,不知道大家是否有新的去處)

    


免責聲明!

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



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