1.检查当前的程序和服务器的最新程序的版本,如果低于服务端的那么才能升级
2.服务端的文件打包.zip文件
3.把压缩包文件解压缩并替换客户端的debug下所有文件。
4.创建另外的程序为了解压缩覆盖掉原始的低版本的客户程序。
有个项目update 负责在应该关闭之后复制解压文件夹 完成更新
这里选择winform项目,项目名update
以下是 update/program.cs
文件的内容:
using system; using system.collections.generic; using system.configuration; using system.diagnostics; using system.io; using system.io.compression; using system.threading; using system.windows.forms; namespace update { internal static class program { private static readonly hashset<string> selffiles = new hashset<string> { "update.pdb", "update.exe", "update.exe.config" }; [stathread] static void main() { string delay = configurationmanager.appsettings["delay"]; thread.sleep(int.parse(delay)); string exepath = null; string path = appdomain.currentdomain.basedirectory; string zipfile = path.combine(path, "update.zip"); try { using (ziparchive archive = zipfile.openread(zipfile)) { foreach (ziparchiveentry entry in archive.entries) { if (selffiles.contains(entry.fullname)) { continue; } string filepath = path.combine(path, entry.fullname); if (filepath.endswith(".exe")) { exepath = filepath; } entry.extracttofile(filepath, true); } } } catch (exception ex) { messagebox.show("升级失败" + ex.message); throw; } if (file.exists(zipfile)) file.delete(zipfile); if (exepath == null) { messagebox.show("找不到可执行文件!"); return; } process process = new process(); process.startinfo = new processstartinfo(exepath); process.start(); } } }
以下是 app.config
文件的内容:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.7.2" /> </startup> <appsettings> <add key="delay" value="3000"/> </appsettings> </configuration>
winform应用
软件版本
[assembly: assemblyfileversion("1.0.0.0")] if (judgeupdate()) { updateapp(); }
检查更新
private bool judgeupdate() { string url = "http://localhost:8275/api/getversion"; string latestversion = null; try { using (httpclient client = new httpclient()) { task<httpresponsemessage> httpresponsemessage = client.getasync(url); httpresponsemessage.wait(); httpresponsemessage response = httpresponsemessage.result; if (response.issuccessstatuscode) { task<string> strings = response.content.readasstringasync(); strings.wait(); jobject jobject = jobject.parse(strings.result); latestversion = jobject["version"].tostring(); } } if (latestversion != null) { var versioninfo = fileversioninfo.getversioninfo(application.executablepath); if (version.parse(latestversion) > version.parse(versioninfo.fileversion)) { return true; } } } catch (exception) { throw; } return false; }
执行更新
public void updateapp() { string url = "http://localhost:8275/api/getzips"; string zipname = "update.zip"; string updateexename = "update.exe"; using (httpclient client = new httpclient()) { task<httpresponsemessage> httpresponsemessage = client.getasync(url); httpresponsemessage.wait(); httpresponsemessage response = httpresponsemessage.result; if (response.issuccessstatuscode) { task<byte[]> bytes = response.content.readasbytearrayasync(); bytes.wait(); string path = appdomain.currentdomain.basedirectory + "/" + zipname; using (filestream fs = new filestream(path, filemode.create, fileaccess.write)) { fs.write(bytes.result, 0, bytes.result.length); } } process process = new process() { startinfo = new processstartinfo(updateexename) }; process.start(); environment.exit(0); } }
服务端
using microsoft.aspnetcore.mvc; using system.diagnostics; using system.io.compression; namespace webapplication1.controllers { [apicontroller] [route("api")] public class clientupdatecontroller : controllerbase { private readonly ilogger<clientupdatecontroller> _logger; public clientupdatecontroller(ilogger<clientupdatecontroller> logger) { _logger = logger; } /// <summary> /// 获取版本号 /// </summary> /// <returns>更新版本号</returns> [httpget] [route("getversion")] public iactionresult getversion() { string? res = null; string zipfile = path.combine(appdomain.currentdomain.basedirectory, "wwwroot", "updatezip", "update.zip"); string exename = null; using (ziparchive archive = zipfile.openread(zipfile)) { foreach (ziparchiveentry entry in archive.entries) { //string filepath = path.combine(path, entry.fullname); if (entry.fullname.endswith(".exe") && !entry.fullname.equals("update.exe")) { entry.extracttofile(path.combine(appdomain.currentdomain.basedirectory, "wwwroot", "updatezip", entry.fullname), true); exename = entry.fullname; } } } fileversioninfo versioninfo = fileversioninfo.getversioninfo(path.combine(appdomain.currentdomain.basedirectory, "wwwroot", "updatezip", exename)); res = versioninfo.fileversion; return ok(new { version = res?.tostring() }); } /// <summary> /// 获取下载地址 /// </summary> /// <returns>下载地址</returns> [httpget] [route("geturl")] public iactionresult geturl() { // var $"10.28.75.159:{publicconfig.serviceport}" return ok(); } /// <summary> /// 获取下载的zip压缩包 /// </summary> /// <returns>下载的zip压缩包</returns> [httpget] [route("getzips")] public async task<iactionresult> getzips() { // 创建一个内存流来存储压缩文件 using (var memorystream = new memorystream()) { // 构建 zip 文件的完整路径 var zipfilepath = path.combine(directory.getcurrentdirectory(), "wwwroot", "updatezip", "update.zip"); // 检查文件是否存在 if (!system.io.file.exists(zipfilepath)) { return notfound("the requested zip file does not exist."); } // 读取文件内容 var filebytes = system.io.file.readallbytes(zipfilepath); // 返回文件 return file(filebytes, "application/zip", "update.zip"); } } } }
到此这篇关于c# winform实现自动更新的文章就介绍到这了,更多相关winform自动更新内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论