詳解C# WinForm如何實(shí)現(xiàn)自動(dòng)更新程序
前言
在C/S這種模式中,自動(dòng)更新程序就顯得尤為重要,它不像B/S模式,直接發(fā)布到服務(wù)器上,瀏覽器點(diǎn)個(gè)刷新就可以了。由于涉及到客戶端文件,所以必然需要把相應(yīng)的文件下載下來(lái)。這個(gè)其實(shí)比較常見(jiàn),我們常用的微信、QQ等,也都是這個(gè)操作。
自動(dòng)更新程序也分為客戶端和服務(wù)端兩部分,客戶端就是用來(lái)下載的一個(gè)小程序,服務(wù)端就是供客戶端調(diào)用下載接口等操作。
這里第一步先將服務(wù)端代碼寫(xiě)出來(lái),邏輯比較簡(jiǎn)單,使用xml文件分別存儲(chǔ)各個(gè)文件的名稱以及版本號(hào)(每次需要更新的時(shí)候,將需要更新的文件上傳到服務(wù)器后,同步增加一下xml文件中對(duì)應(yīng)的版本號(hào))。然后比對(duì)客戶端傳進(jìn)來(lái)的文件版本,若服務(wù)端版本比較高,則加入到下載列表中??蛻舳嗽傺h(huán)調(diào)用下載列表中的文件進(jìn)行下載更新。
開(kāi)發(fā)環(huán)境
.NET Core 3.1
開(kāi)發(fā)工具
Visual Studio 2019
實(shí)現(xiàn)代碼
//xml文件 <?xml version="1.0" encoding="utf-8" ?> <updateList> <url>http://localhost:5000/api/Update/</url> <files> <file name="1.dll" version="1.0"></file> <file name="1.dll" version="1.1"></file> <file name="AutoUpdate.Test.exe" version="1.1"></file> </files> </updateList>
//Model public class UpdateModel { public string name { get; set; } public string version { get; set; } } public class UpdateModel_Out { public string url { get; set; } public List<UpdateModel> updateList { get; set; } }
//控制器 namespace AutoUpdate.WebApi.Controllers { [Route("api/[controller]/[Action]")] [ApiController] public class UpdateController : ControllerBase { [HttpGet] public JsonResult Index() { return new JsonResult(new { code = 10, msg = "success" }); } [HttpPost] public JsonResult GetUpdateFiles([FromBody] List<UpdateModel> input) { string xmlPath = AppContext.BaseDirectory + "UpdateList.xml"; XDocument xdoc = XDocument.Load(xmlPath); var files = from f in xdoc.Root.Element("files").Elements() select new { name = f.Attribute("name").Value, version = f.Attribute("version").Value }; var url = xdoc.Root.Element("url").Value; List<UpdateModel> updateList = new List<UpdateModel>(); foreach(var file in files) { UpdateModel model = input.Find(s => s.name == file.name); if(model == null || file.version.CompareTo(model.version) > 0) { updateList.Add(new UpdateModel { name = file.name, version = file.version }); } } UpdateModel_Out output = new UpdateModel_Out { url = url, updateList = updateList }; return new JsonResult(output); } [HttpPost] public FileStreamResult DownloadFile([FromBody] UpdateModel input) { string path = AppContext.BaseDirectory + "files\\" + input.name; FileStream fileStream = new FileStream(path, FileMode.Open); return new FileStreamResult(fileStream, "application/octet-stream"); } } }
實(shí)現(xiàn)效果
只有服務(wù)端其實(shí)沒(méi)什么演示的,這里先看一下更新的效果吧。
代碼解析
就只介紹下控制器中的三個(gè)方法吧,Index其實(shí)沒(méi)什么用,就是用來(lái)做個(gè)測(cè)試,證明服務(wù)是通的;GetUpdateFiles用來(lái)比對(duì)版本號(hào),獲取需要更新的文件(這里用到了Linq To Xml 來(lái)解析xml文件,之前文章沒(méi)寫(xiě)過(guò)這個(gè)方式,后面再補(bǔ)下);DownloadFile就是用來(lái)下載文件的了。
到此這篇關(guān)于詳解C# WinForm如何實(shí)現(xiàn)自動(dòng)更新程序的文章就介紹到這了,更多相關(guān)C# WinForm自動(dòng)更新程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
![C# Stream 和 byte[] 之間的轉(zhuǎn)換](http://img.jbzj.com/images/xgimg/bcimg2.png)
C# Stream 和 byte[] 之間的轉(zhuǎn)換

C#控制臺(tái)基礎(chǔ) list<>初始化的兩種方法

C#實(shí)現(xiàn)Base64編碼與解碼及規(guī)則

使用C#開(kāi)發(fā)OPC?Server服務(wù)器源碼解析

C#實(shí)現(xiàn)驗(yàn)證身份證是否合法的方法