更新于:2019-11-07
C#自动更新程序
下载更新配置文件
xdoc = XDocument.Load("https://github.com/../releases/download/v10/version.xml");//版本配置文件地址
更新配置文件模板
<update minimum='1.0.2.4' latest='1.0.2.4'>
<release version='1.0.2.4' file='https://github.com/../releases/download/v2.1/Release.zip'>
<changes>***</changes>
</release>
</update>
获取当前程序版本
Winform
Version version = Assembly.GetExecutingAssembly().GetName().Version;
Netcore
Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
获取配置文件内容
Version minimum = Version.Parse(xdoc.Element("update").Attribute("minimum").Value);
下载更新文件
WebClient web = new WebClient();
winform
web.DownloadFileCompleted += Web_DownloadFileCompleted;
web.DownloadFileAsync(new Uri("远程地址"), "目标地址");
Netcore
web.DownloadFileCompleted += Web_DownloadFileCompleted;
web.DownloadFile(new Uri("远程地址"), "目标地址");
解压
ZipFile.ExtractToDirectory(download_path, di.Name);
结束当前进程
Winform
重写更新文件
File.WriteAllBytes("update.bat", Resources.UpdateBat);
关闭进程
Close();
if (Program.MainForm != null) Program.MainForm.Close();
Netcore
Process.