You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

96 lines
3.6 KiB

using PCScreenSavers.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace PCScreenSavers.Business
{
class VersionUpdate
{
/// <summary>
/// 是否需要更新
/// </summary>
/// <returns></returns>
public List<XmlNodeItem> IsUpdate(out string currentVersion,string conUpdate)
{
List<XmlNodeItem> updateList = new List<XmlNodeItem>();
try
{
string localPath = Directory.GetCurrentDirectory() + "\\ScreenVersion.txt";
FileStream fs = new FileStream(localPath, FileMode.Open);
using (StreamReader sr = new StreamReader(fs))
{
string temp = sr.ReadLine();
currentVersion = temp.Substring(temp.LastIndexOf("V"));
}
//fs.Close();
List<XmlNodeItem> versionList = XmlReader(conUpdate);
foreach (var item in versionList)
{
if (currentVersion != item.Title)
updateList.Add(item);
else
break;
}
return updateList;
}
catch
{
currentVersion = "";
return updateList;
}
}
/// <summary>
/// 读取xml文件
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private List<XmlNodeItem> XmlReader(string url)
{
List<XmlNodeItem> nodeList = new List<XmlNodeItem>();
try
{
XmlDocument doc = new XmlDocument();
doc.Load(url); //加载Xml文件
XmlElement rootElem = doc.DocumentElement; //获取根节点
XmlNodeList itemNodes = rootElem.GetElementsByTagName("item"); //获取item子节点集合
foreach (XmlNode node in itemNodes)
{
XmlNodeItem item = new XmlNodeItem();
XmlNodeList subTitleNodes = ((XmlElement)node).GetElementsByTagName("title"); //获取title子XmlElement集合
XmlNodeList subDateNodes = ((XmlElement)node).GetElementsByTagName("date"); //获取date子XmlElement集合
XmlNodeList subUrlNodes = ((XmlElement)node).GetElementsByTagName("url"); //获取url子XmlElement集合
XmlNodeList subConfigNodes = ((XmlElement)node).GetElementsByTagName("iscopyconfig"); //获取iscopyconfig子XmlElement集合
XmlNodeList subDescribtionNodes = ((XmlElement)node).GetElementsByTagName("describtion"); //获取describtion子XmlElement集合
if (subTitleNodes.Count == 1)
item.Title = subTitleNodes[0].InnerText;
if (subDateNodes.Count == 1)
item.Date = subDateNodes[0].InnerText;
if (subUrlNodes.Count == 1)
item.Url = subUrlNodes[0].InnerText;
if (subConfigNodes.Count == 1)
item.IsCopyConfig = subConfigNodes[0].InnerText;
if (subDescribtionNodes.Count == 1)
item.Describtion = subDescribtionNodes[0].InnerText;
nodeList.Add(item);
}
return nodeList;
}
catch (Exception ex)
{
return nodeList;
}
}
}
}