[C#] 检查是否需要更新 →→→→→进入此内容的聊天室

来自 , 2020-12-22, 写在 C#, 查看 224 次.
URL http://www.code666.cn/view/75455e06
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.IO;
  8. using System.Xml;
  9. using System.Reflection;
  10.  
  11. namespace MyDll
  12. {
  13.     public class CheckUpdate
  14.     {
  15.         public string updateXml = "http://192.168.1.105:8067/UpdateList.xml";    //升级配置的XML文件地址
  16.         public string downloadUrl = "";                                                                    //文件下载地址
  17.         public string filePath = "";                                                                             //要检查更新的文件路径
  18.         public string fileName = "";                                                                          //要更新的文件名
  19.         public string newVersion = "";                                                                      //文件新版本号
  20.         public bool isUpdate;                                                                                    //是否更新
  21.  
  22.         /// <summary>
  23.         /// 构造函数
  24.         /// </summary>
  25.         /// <param name="filePath"></param>
  26.         /// <param name="fileName"></param>
  27.         public CheckUpdate(string filePath, string fileName)
  28.         {
  29.             this.filePath = filePath;
  30.             this.fileName = fileName;
  31.         }
  32.  
  33.         /// <summary>
  34.         /// 检查是否要更新
  35.         /// </summary>
  36.         public bool IsUpdate()
  37.         {
  38.             try
  39.             {
  40.                 WebClient wc = new WebClient();
  41.                 Stream stream = wc.OpenRead(updateXml);         //打开一个可读流,用来下载资源
  42.                 XmlDocument xmlDoc = new XmlDocument();     //表示xml文档
  43.                 xmlDoc.Load(stream);                                             //从指定的流加载xml文档
  44.                 XmlNode nodelist = xmlDoc.SelectSingleNode("Update");
  45.                 foreach (XmlNode node in nodelist)
  46.                 {
  47.                     if (node.Name == "Soft" && node.Attributes["Name"].Value.ToLower() == fileName.ToLower())
  48.                     {
  49.                         foreach (XmlNode xmlnode in node)
  50.                         {
  51.                             if (xmlnode.Name == "Version")
  52.                             {
  53.                                 newVersion = xmlnode.InnerText;
  54.                             }
  55.                             else
  56.                             {
  57.                                 downloadUrl = xmlnode.InnerText;
  58.                             }
  59.                         }
  60.                     }
  61.                 }
  62.                 Version ver = new Version(newVersion);      //新版本号
  63.                 Version version = Assembly.LoadFrom(fileName).GetName().Version;
  64.                 int comp = version.CompareTo(ver);
  65.                 if (comp >= 0)
  66.                 {
  67.                     isUpdate = false;
  68.                 }
  69.                 else
  70.                 {
  71.                     isUpdate = true;
  72.                 }
  73.                 return isUpdate;
  74.             }
  75.             catch (Exception ex)
  76.             {
  77.                 throw new Exception("更新出现错误,请确认网络连接无误后重试!");
  78.             }
  79.         }
  80.     }
  81. }
  82.  
  83.  
  84.  
  85. //对应XML文件
  86. <?xml version="1.0" encoding="utf-8" ?>
  87. <Update>
  88.   <Soft Name="LMS.exe">
  89.     <Version>1.0.1.7</Version>
  90.     <DownLoad>http://192.168.1.105:8067/Update/Debug/LMS.exe</DownLoad>
  91.   </Soft>
  92. </Update>
  93.  

回复 "检查是否需要更新"

这儿你可以回复上面这条便签

captcha