自从接触安装部署以来就有软件升级的需求,最简单的就是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 = @"....skinEmeraldColor2.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这个函
