using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoUpgrade.Models;
using System.Xml.Linq;
using System.Net;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Diagnostics;
namespace AutoUpgrade.BL
{
public class XmlFileHandle
{
public List<UpdataFileModel> updataFileList = null;
public List<UpdataFileModel> interUpdataFileList = null;
public List<UpdataFileModel> localUpdataFileList = null;
private string localRootPath = string.Empty;
private string ReleaseAddress = string.Empty;
private string InternetUpdateFile = string.Empty;
private string LocalUpdateFile = string.Empty;
private static XmlFileHandle _instance = null;
public static XmlFileHandle Instance()
{
if (_instance == null)
{
_instance = new XmlFileHandle();
}
return _instance;
}
public XmlFileHandle()
{
localRootPath = Application.StartupPath;
ReleaseAddress = ConfigurationManager.AppSettings["ReleaseAddress"];
InternetUpdateFile = ConfigurationManager.AppSettings["InternetUpdateFile"];
LocalUpdateFile = ConfigurationManager.AppSettings["LocalUpdateFile"];
}
#region 检查更新文件是否有新的补丁
/// <summary>
/// 检查更新文件是否有新的补丁
/// </summary>
/// <returns></returns>
public bool CheckUpdateFile()
{
DateTime localupDate = GetLocalUpdateFileReleaseTime();
DateTime internetUpDate = GetInterUpdateFileReleaseTime();
if (DateTime.Compare(internetUpDate, localupDate) > 0)
{
LoadLocalUpdateFileAll();
return true;
}
return false;
}
private DateTime GetLocalUpdateFileReleaseTime()
{
string path = localRootPath + "\\" + LocalUpdateFile;
return GetUpdateFileReleaseTime(path);
}
private DateTime GetInterUpdateFileReleaseTime()
{
string path = InternetUpdateFile;
return GetUpdateFileReleaseTime(path);
}
private DateTime GetUpdateFileReleaseTime(string strpath)
{
if (string.IsNullOrEmpty(strpath))
return DateTime.MinValue;
try
{
XElement root = XElement.Load(strpath);
if (!string.IsNullOrEmpty(root.Attribute("ReleaseTime").Value))
return Convert.ToDateTime(root.Attribute("ReleaseTime").Value);
}
catch (Exception ex)
{
throw ex;
}
return DateTime.MinValue;
}
#endregion
#region 读取更新文件到数据集合List<FolderInfoModel>中
/// <summary>
/// 获取Internet文件列表并下载
/// </summary>
private List<FolderInfoModel> ReadInternetUpdateList()
{
string path = InternetUpdateFile;
return ReadUpdateList(path);
}
/// <summary>
/// 获取Local文件列表
/// </summary>
/// <returns></returns>
private List<FolderInfoModel> ReadLocalUpdateList()
{
string path = localRootPath + "\\" + LocalUpdateFile;
return ReadUpdateList(p