using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Text; |
using System.Threading.Tasks; |
using System.Net; |
using System.IO; |
using System.Xml; |
using System.Reflection; |
namespace MyDll |
{ |
public class CheckUpdate |
{ |
public string updateXml = "http://192.168.1.105:8067/UpdateList.xml" ; //升级配置的XML文件地址 |
public string downloadUrl = "" ; //文件下载地址 |
public string filePath = "" ; //要检查更新的文件路径 |
public string fileName = "" ; //要更新的文件名 |
public string newVersion = "" ; //文件新版本号 |
public bool isUpdate; //是否更新 |
/// <summary> |
/// 构造函数 |
/// </summary> |
/// <param name="filePath"></param> |
/// <param name="fileName"></param> |
public CheckUpdate( string filePath, string fileName) |
{ |
this .filePath = filePath; |
this .fileName = fileName; |
} |
/// <summary> |
/// 检查是否要更新 |
/// </summary> |
public bool IsUpdate() |
{ |
try |
{ |
WebClient wc = new WebClient(); |
Stream stream = wc.OpenRead(updateXml); //打开一个可读流,用来下载资源 |
XmlDocument xmlDoc = new XmlDocument(); //表示xml文档 |
xmlDoc.Load(stream); //从指定的流加载xml文档 |
XmlNode nodelist = xmlDoc.SelectSingleNode( "Update" ); |
foreach (XmlNode node in nodelist) |
{ |
if (node.Name == "Soft" && node.Attributes[ "Name" ].Value.ToLower() == fileName.ToLower()) |
{ |
foreach (XmlNode xmlnode in node) |
{ |
if (xmlnode.Name == "Version" ) |
{ |
newVersion = xmlnode.InnerText; |
} |
else |
{ |
downloadUrl = xmlnode.InnerText; |
} |
} |
} |
} |
Version ver = new Version(newVersion); //新版本号 |
Version version = Assembly.LoadFrom(fileName).GetName().Version; |
int comp = version.CompareTo(ver); |
if (comp >= 0) |
{ |
isUpdate = false ; |
} |
else |
{ |
isUpdate = true ; |
} |
return isUpdate; |
} |
catch (Exception ex) |
{ |
throw new Exception( "更新出现错误,请确认网络连接无误后重试!" ); |
} |
} |
} |
} |
//对应XML文件 |
<?xml version= "1.0" encoding= "utf-8" ?> |
<Update> |
<Soft Name= "LMS.exe" > |
<Version>1.0.1.7</Version> |
<DownLoad>http: //192.168.1.105:8067/Update/Debug/LMS.exe</DownLoad> |
</Soft> |
</Update> |
by: 发表于:2018-01-08 10:19:14 顶(0) | 踩(0) 回复
??
回复评论