前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >详解C# WinForm如何实现自动更新程序的案例分享

详解C# WinForm如何实现自动更新程序的案例分享

原创
作者头像
用户7718188
发布2022-11-06 20:19:56
1.4K0
发布2022-11-06 20:19:56
举报
文章被收录于专栏:高级工程司高级工程司

实现代码

//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");

}

}

}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档