目录
GitHub:https://github.com/ravibpatel/AutoUpdater.NET
NuGet: PM> Install-Package Autoupdater.NET.Official
AutoUpdater.NET 是一个类库。它能够给DOT NET程序增加自动更新功能。
1. 一般工作流程
- AutoUpdater.NET从服务器上下载一个包含更新信息的XML文件
- 通过该XML文件获取软件最新版本信息
- 如果最新版本大于当前电脑上安装的软件版本,将显示一个更新对话框
- 如果用户按下更新按钮,将从XML文件的URL属性指示的位置下载最新的安装文件,并在下载完成后执行更新。如果提供的是一个ZIP压缩包,AutoUpdater.NET将提取ZIP文件的内容到应用程序目录
2. XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<item>
<!--版本号,必选-->
<version>2.0.0.0</version>
<!--更新文件,必选-->
<url>http://rbsoft.org/downloads/AutoUpdaterTest.zip</url>
<!--更新日志,可选-->
<changelog>https://github.com/ravibpatel/AutoUpdater.NET/releases</changelog>
<!--强制更新-->
<mandatory>false</mandatory>
<mandatory mode="1">true</mandatory>
<mandatory mode="2">true</mandatory>
<mandatory minVersion="1.2.0.0">true</mandatory>
<!--安装程序的命令行参数-->
<args></args>
<!--更新校验-->
<checksum algorithm="MD5">Update file Checksum</checksum>
</item>
2.1 标签说明
- version(必选):在该标签中提供最新的程序集版本号。格式X.X.X.X
- url(必选):最新版本的安装文件或ZIP文件的URL路径。当用户点击更新按钮后,AutoUpdater.NET将下载指定的文件。
- changelog(可选):更新日志文件的URL路径。如果提供,将在更新对话框中显示更新日志
- mandatory(可选):强制更新。如果不希望用户跳过该版本,设置为true。这将忽略【稍后提醒】和【跳过该版本】选项,同时隐藏更新对话框上的【稍后提醒】和【跳过该版本】按钮
- mode(属性,可选):通过该属性控制强制更新的行为。如果该值设置为1,将隐藏更新对话框上的更新按钮。如果设置为2,则跳过更新对话框,自动的开始下载和更新应用程序
- minVersion(属性,可选):仅仅在已安装的程序集版本号小于指定的最小版本号,才会执行强制更新
- args(可选):提供安装文件运行时需要的命令行参数。%path%代表应用程的当前路径
- checksum(可选):更新校验。在更新程序之前,通过指定的文件校验码校验所下载的文件是否完整。目前支持以下校验算法:MD5, SHA1, SHA256, SHA384, and SHA512
3. 使用方法
- 导入命名空间:
using AutoUpdaterDotNET;
- 在合适的位置放置以下代码,方法参数指定了XML配置文件的URL路径:
AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.xml");
4. 相关说明
4.1 查看当前程序集的版本号
- 项目属性 >> 应用程序标签 >> 程序集信息(I)...按钮
也可以通过之指定程序集,用以获取当前的版本信息。
AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.xml", myAssembly);
4.2 通过FTP Server进行更新
AutoUpdater.Start("ftp://rbsoft.org/updates/AutoUpdaterTest.xml", new NetworkCredential("FtpUserName", "FtpPassword"));
4.3 同步更新(默认异步)
AutoUpdater.Synchronous = true;
4.4 禁用跳过按钮
AutoUpdater.ShowSkipButton = false;
4.5 禁用稍后提醒
AutoUpdater.ShowRemindLaterButton = false;
4.6 禁用先前设置的稍后提醒和跳过选项
// 设置为true,将隐藏跳过和稍后提醒按钮,且XML配置文件中Mandatory值被忽略
AutoUpdater.Mandatory = true;
4.7 强制更新
你可以使能强制更新,拼接设置Mandatory=true和UpdateMode=Mode.Forced/Mode.ForcedDownload。
- Mode.Forced:在标准的更新对话框上隐藏稍后提醒,跳过版本,关闭按钮
- Mode.ForcedDownload:跳过标准更新对话框和启动下载页面,并忽略OpenDownloadPage标识设置
AutoUpdater.Mandatory = true;
AutoUpdater.UpdateMode = Mode.Forced;
4.8 基本的身份认证
BasicAuthentication basicAuthentication = new BasicAuthentication("myUserName", "myPassword");
AutoUpdater.BasicAuthXML = AutoUpdater.BasicAuthDownload = AutoUpdater.BasicAuthChangeLog = basicAuthentication;
4.9 用户代理
AutoUpdater.HttpUserAgent = "AutoUpdater";
4.10 错误报告
在无可用更新或无法从服务器上下载XML配置文件,将提示错误信息
AutoUpdater.ReportErrors = true;
4.11 关闭管理员权限
AutoUpdater.RunUpdateAsAdmin = false;
4.12 查看更新而不下载
AutoUpdater.OpenDownloadPage = true;
4.13 稍后提醒
如果不希望用户在点击稍后提醒按钮后,选择稍后提醒的时间。
AutoUpdater.LetUserSelectRemindLater = false;
AutoUpdater.RemindLaterTimeSpan = RemindLaterFormat.Days;
AutoUpdater.RemindLaterAt = 2;
4.14 代理服务器
var proxy = new WebProxy("ProxyIP:ProxyPort", true)
{
Credentials = new NetworkCredential("ProxyUserName", "ProxyPassword")
};
AutoUpdater.Proxy = proxy;
4.15 指定下载文件的存储目录
默认为系统临时文件夹
AutoUpdater.DownloadPath = Environment.CurrentDirectory;
4.16 指定解压ZIP文件的目录
默认解压到ZIP文件的同级目录
var currentDirectory = new DirectoryInfo(Environment.CurrentDirectory);
if (currentDirectory.Parent != null)
{
AutoUpdater.InstallationPath = currentDirectory.Parent.FullName;
}
4.17 指定更新窗口的尺寸
AutoUpdater.UpdateFormSize = new System.Drawing.Size(800, 600);
4.18 改变稍后提醒和跳过选项的存储方式
string jsonPath = Path.Combine(Environment.CurrentDirectory, "settings.json");
AutoUpdater.PersistenceProvider = new JsonFilePersistenceProvider(jsonPath);
4.19 设置更新检查的频率
System.Timers.Timer timer = new System.Timers.Timer
{
Interval = 2 * 60 * 1000,
SynchronizingObject = this
};
timer.Elapsed += delegate
{
AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.xml");
};
timer.Start();
DispatcherTimer timer = new DispatcherTimer {Interval = TimeSpan.FromMinutes(2)};
timer.Tick += delegate
{
AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTestWPF.xml");
};
timer.Start();
4.20 更改应用程序退出逻辑
AutoUpdater.ApplicationExitEvent += AutoUpdater_ApplicationExitEvent;
private void AutoUpdater_ApplicationExitEvent()
{
Text = @"Closing application...";
Thread.Sleep(5000);
Application.Exit();
}
4.21 更改更新方式
AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent;
private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args)
{
if (args != 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
{
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);
}
}
4.22 更改XML配置文件
AutoUpdater.ParseUpdateInfoEvent += AutoUpdaterOnParseUpdateInfoEvent;
AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.json");
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
}
};
}
{
"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"
}
}
5. 样例
5.1 相关文件准备
5.1.1. 创建新版本程序集
打开测试应用程序将程序集版本号修改为2.0.0.0,并用ZIP进行打包
5.1.2. 创建更新日志文件
经测试TXT文件和HTML文件均能被识别
<!DOCTYPE html>
<html lang="zh-CN" data-blockbyte-bs-uid="22649">
<head>
<title>UpdateTest</title>
</head>
<body>
<strong>Update Infos</strong><br>
<ul>
<li>Code Optimization</li>
<li>Added way to change how Skip and Remind Later values are preserved</li>
<li>Added ability to execute update check synchronously</li>
<li>Added minimum version mandatory version check</li>
</ul>
</div>
</body>
</html>
Code Optimization
Added way to change how Skip and Remind Later values are preserved.
Added ability to execute update check synchronously.
Added minimum version mandatory version check.
5.1.3. 创建更新配置文件
<item>
<version>2.0.0.0</version>
<url>ftp://127.0.0.1/AutoUpdaterTest.zip</url>
<changelog>ftp://127.0.0.1/changelog.html</changelog>
<mandatory>false</mandatory>
</item>
5.1.4. 集中存放
将这些文件集中放置在一指定的目录中
5.2 通过FTP更新
5.2.1 搭建FTP服务器
这里使用FileZilla Server。默认安装后,打开FTP服务器。点击工具栏上的用户按钮
在General选项卡中,Users区域点击Add按钮,在弹出的对话框中键入一个用于登录服务器的用户名
设置用户的登录密码,这里设置为open
在Shared folders选项卡中,Shared folders区域,点击Add按钮,在弹出的对话框中选择一个作为FTP服务器根目录的文件夹
到此,FTP服务器搭建完毕。
5.2.2 自动更新代码
在程序的窗体事件中添加以下代码
// 设置下载目录
AutoUpdater.DownloadPath = Environment.CurrentDirectory;
AutoUpdater.Start("ftp://127.0.0.1/AutoUpdater.xml", new NetworkCredential("open", "open"));
5.2.3 更新过程
编译运行后,首先弹出程序主窗体,接着弹出更新对话框。当点击更新按钮后,后台开始从服务器上下载文件,解压,安装,运行。
更新完成后,弹出新版本的程序运行结果
5.3 Web服务器更新
5.3.1 搭建Web服务器
这里使用CassiniDev Server。运行该程序,并点击Physical Path标签右下角的按钮,选择服务器的根目录。
点击右下角的Start按钮,启动服务器。圈出来的时Web服务器的地址
5.3.2 自动更新代码
在程序中,将FTP更新,更改为WEB更新。
AutoUpdater.Start("http://localhost:32768/AutoUpdater.xml");
其余操作与FTP相同,不在赘述。