AutoUpdater.NET 客户端更新指定文件,具体使用如下
1、新建项目windows窗体项目--WindowsFormTestUpdate
2、添加NuGet程序管理包
选择项目右键,选择“管理NuGet程序包”,搜索“Autoupdater.NET.Official”,选择,点击安装
3、窗体代码如下:
界面
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using AutoUpdaterDotNET;
using Newtonsoft.Json;
namespace WindowsFormTestUpdate
{
public partial class TestUpdateForm : Form
{
public TestUpdateForm()
{
InitializeComponent();
}
private void TestUpdateForm_Load(object sender, EventArgs e)
{
lblVersion.Text = "版本:" + Application.ProductName + Application.ProductVersion;
}
private void btnAutoUpdate_Click(object sender, EventArgs e)
{
AutoUpdater.ReportErrors = true;
//1、根据服务端的更新文件update.xml的version检测是否与当前应用程序版本一致,是否需要更新。
//AutoUpdater.Start("http://localhost:8082/TestAutoUpdate/update.xml");
//2、根据服务端的更新文件update.xml的version检测是否与指定myAssembly应用程序版本一致,是否需要更新。
//AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.xml", System.Reflection.Assembly.GetExecutingAssembly()/*this.GetType().Assembly*/);
//3、根据FTP服务端的更新文件update.xml的version检测是否与指定myAssembly应用程序版本一致,是否需要更新。
//AutoUpdater.Start("ftp://127.0.0.1/TestAutoUpdate/update.xml", new NetworkCredential("myuser", "mypassword"));
//其他属性设置
//设置更新弹窗的大小
//AutoUpdater.UpdateFormSize = new System.Drawing.Size(800, 600);
AutoUpdater.AppCastURL = "";//服务端的更新文件update.xml
AutoUpdater.InstalledVersion = new Version("1.3");
//AutoUpdater.AppTitle = "更新弹窗";//弹出窗体标题
AutoUpdater.DownloadPath = "";//升级下载包路径
AutoUpdater.PersistenceProvider = "";//升级下载包路径
AutoUpdater.Mandatory = true;
AutoUpdater.Mandatory = true;
AutoUpdater.UpdateMode = Mode.Forced;
//AutoUpdater.ShowRemindLaterButton = true;//显示稍后提醒按钮
AutoUpdater.LetUserSelectRemindLater = false;
AutoUpdater.RemindLaterTimeSpan = RemindLaterFormat.Days;
AutoUpdater.RemindLaterAt = 2;
//AutoUpdater.ShowSkipButton = true;//显示稍后提醒按钮
//AutoUpdater.Synchronous = true;//显示稍后提醒按钮
var proxy = new WebProxy("ProxyIP:ProxyPort", true)
{
Credentials = new NetworkCredential("ProxyUserName", "ProxyPassword")
};
AutoUpdater.Proxy = proxy;
设置凭证
BasicAuthentication basicAuthentication = new BasicAuthentication("myUserName", "myPassword");
AutoUpdater.BasicAuthXML = AutoUpdater.BasicAuthDownload = AutoUpdater.BasicAuthChangeLog = basicAuthentication;
浏览器打开下载
AutoUpdater.OpenDownloadPage = true;
//string jsonPath = Path.Combine(Environment.CurrentDirectory, "settings.json");
//AutoUpdater.PersistenceProvider = new JsonFilePersistenceProvider(jsonPath);
Handling Application exit logic manually,自定义更新成功之后操作
//AutoUpdater.ApplicationExitEvent += AutoUpdater_ApplicationExitEvent;
//自定义更新
//AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent;
//自定义更新文件转换,可以指定json类型的文件,如下
//AutoUpdater.ParseUpdateInfoEvent += AutoUpdaterOnParseUpdateInfoEvent;
//AutoUpdater.Start("http://localhost:8082/AutoUpdaterTest.json");
#region 指定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"
//}
// }
#endregion
//Text = Application.StartupPath;
AutoUpdater.Start("http://localhost:8082/TestAutoUpdate/update.xml");
}
private void AutoUpdater_ApplicationExitEvent()
{
//Text = @"Closing application...";
Thread.Sleep(5000);
//Application.Exit();
//string exepath = @"C:\Users\Administrator\Desktop\Debug\AutoUpdaterTest";
//var currentDirectory = new DirectoryInfo(Application.StartupPath);
//if (currentDirectory.Parent != null)
//{
// exepath = Path.Combine(currentDirectory.Parent.FullName, "TestAutoUpdate", "WindowsFormTestUpdate.exe");
//}
//lblVersion.Text = lblVersion.Text + $"----{exepath}";
//System.Diagnostics.Process.Start(exepath);
}
private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args)
{
if (args.Error == 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
{
if (args.Error is WebException)
{
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);
}
else
{
MessageBox.Show(args.Error.Message,
args.Error.GetType().ToString(), MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
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
}
};
}
}
}
注意:
1、 获取配置文件方式可以是网站服务器,也可以是ftp服务器,配置文件可以是xml的,当然也可以是其他格式的,但是其他格式的,如json,获取方式就不一样了(AutoUpdater.Start("http://localhost:8082/AutoUpdaterTest.json"))
一般是使用如下:
//1、根据服务端的更新文件update.xml的version检测是否与当前应用程序版本一致,是否需要更新。
AutoUpdater.Start("http://localhost:8082/TestAutoUpdate/update.xml");
//2、根据服务端的更新文件update.xml的version检测是否与指定myAssembly应用程序版本一致,是否需要更新。
AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.xml", System.Reflection.Assembly.GetExecutingAssembly()/*this.GetType().Assembly*/);
//3、根据FTP服务端的更新文件update.xml的version检测是否与指定myAssembly应用程序版本一致,是否需要更新。
AutoUpdater.Start("ftp://127.0.0.1/TestAutoUpdate/update.xml", new NetworkCredential("myuser", "mypassword"));
2、程序集版本(不同版本的程序集可以通过程序集版本来控制,以示区别)
4、运行结果
5、其他的一些配置可以参考:
https://github.com/ravibpatel/AutoUpdater.NET