实现思路:在主程序打开前实现判断是否需要更新(可以通过数据库表记录一下版本号或者别的方式记录是否需要更新),若需要更新时从ftp站点下载更新包(关于配置ftp站点自己可以搜这里不再做详述)。自己可以制定后缀格式的包或者别的!一般用压缩包的形式来存放最新程序,将文件下载到本地路径,在关闭当前程序打开更新程序做解压替换文件操作,或者可以用批处理文、可执行文件来做操作都行!
1.判断是否有新版本。
2.通过ftp将更新包下载至本地路径。
3.打开更新程序(批处理文件或可执行文件)同时关闭所有主程序进程。
4.在更新程序中进行解压、替换操作。
5.待替换完毕删除本地更新包(可选)。
6.打开新程序同时关闭所有更新程序进程。
代码·:
1.在程序入口处Program.cs中做判断:
//判断版本号是否为数据库表的版本号
if (edition == "2021.5.12.0.1")//版本号自己可以判断
{
var resulta = MessageBox.Show("有可用更新,是否更新?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (resulta == DialogResult.No)
{
Application.Run(new Form1());
return;
}
//从服务器获取新压缩文件后下载至某一路径
UpdatateHelp help = new UpdatateHelp();//更新类
help.IP = "xxx.xx.xx.xxx";
help.ServerFile = "OldDemoUpd.zip";
help.User = "Administrator";
help.Password = "*****";
string message = string.Empty;
if (!help.DownloadFile(out message))
{
var result = MessageBox.Show(message, "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
Application.Run(new Form1());
return;
}
else
{
//强制关闭进程
var proc = System.Diagnostics.Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
foreach (Process item in proc)
{
item.Kill();
}
}
}
//替换程序文件(用一个update程序负责解压程序并替换文件,在删除压缩文件)
System.Diagnostics.Process.Start(Application.StartupPath + "\\Update\\" + "AutoUpdate.exe");
//关闭当前进程
foreach (Process item