我尝试通过 zip 存档创建程序的自动更新。为此,我使用 DotNetZip (Ionic.Zip.dll)。大约自动更新是这样发生的:从站点(版本号所在的站点)下载测试文档,如果版本大于其中的版本,则读取该Preferences.Settings.Default.ver版本
Preferences.Settings.Default.ver = ver:
WebClient wc = new WebClient();
string url = "http://f0160057.xsph.ru/osiddev/app/version.txt";
string file = "version.txt";
wc.DownloadFile(url, file);
StreamReader sr = new StreamReader("version.txt");
ver = int.Parse(sr.ReadLine());
存档 (update.zip) 已下载并启动DDctD_UpTW.exe(主 exe 在之后关闭Process...Kill();:
ver = int.Parse(sr.ReadLine());
if (Properties.Settings.Default.ver < ver)
{
MessageBox.Show("New version available");
wc.DownloadFileAsync(new Uri(@"http://f0160057.xsph.ru/osiddev/app/update.zip"), "update.zip");
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(download_Completed);
}
else
{
MessageBox.Show("You have newest version");
this.Hide();
Window login = new Login();
login.ShowDialog();
this.Close();
}
private void download_Completed(object sender, AsyncCompletedEventArgs e)
{
ver = ver + 1;
Properties.Settings.Default.ver = ver;
Process.Start("DDctD_UpTW.exe");
Process.GetCurrentProcess().Kill();
}
在DDctD_UpTW.exe解压缩存档并替换文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ionic.Zip;
using System.Diagnostics;
namespace DDctD_UpTW
{
class Program
{
static void Main(string[] args)
{
Unzip();
}
public static void Unzip()
{
try
{
using (ZipFile zip = ZipFile.Read("update.zip"))
{
zip.ExtractAll("/", ExtractExistingFileAction.OverwriteSilently);
}
}
catch
{
}
Process.Start("MainExe.exe");
Environment.Exit(0);
}
}
}
但主要问题:
解压缩并运行主 exe 后,它再次写入"New update available". 如何解决?
您不允许通过终止进程将您的设置“保存”到磁盘。通过杀死一个进程
一般不好的做法。从应用程序“定期”退出更正确,然后垃圾收集器将工作,特别是设置将被保存。
但是,您也可以手动保存设置:
但是,如果你这样做,它也将是“不好的”。如果您
DDctD_UpTW.exe的更新失败,则设置与程序的实际状态之间会出现不同步。从比设置更“可靠”的来源更新后检查应用程序的版本更正确。