使用AutoUpdater.NET实现自动更新
AutoUpdater.NET是一个.net库,允许winforms和wpf应用实现自动更新,只需要以下几步:
1,定义一个升级文件;
<?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>
</item>
2, 安装 Autoupdater.NET.Official扩展
PM> Install-Package Autoupdater.NET.Official
3,使用只需要在程序加载前加入以下代码即可
protected override void OnStartup(StartupEventArgs e)
{
AutoUpdater.ParseUpdateInfoEvent += AutoUpdaterOnParseUpdateInfoEvent;
//AutoUpdater.ReportErrors = true;
AutoUpdater.Start("https://api.yunyin.la/file-helper/upgrade");
}
将xml修改为json
private void AutoUpdaterOnParseUpdateInfoEvent(ParseUpdateInfoEventArgs args)
{
dynamic json = JsonConvert.DeserializeObject(args.RemoteData);
args.UpdateInfo = new UpdateInfoEventArgs
{
CurrentVersion = json.version,
DownloadURL = json.url,
Mandatory = new Mandatory
{
Value = json.mandatory.value,
UpdateMode = json.mandatory.mode,
}
};
}
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"
}
}