using Muchinfo.PC.Common.Helpers; using Muchinfo.MTPClient.Infrastructure.Utilities; using System; using System.IO; using System.Net; using System.Xml; namespace Muchinfo.MTPClient.Infrastructure.Helpers { public class VersionManager { readonly XmlDocument serverXmlDoc = new XmlDocument(); private string _folderName = ApplicationParameter.UpdateFolderName; public string FolderName { get { return _folderName; } } public string GetServerVersion() { string newVersion = string.Empty; try { string configPath = GetConfigPath(); XmlDocument doc = new XmlDocument(); doc.Load(configPath); XmlNode versionNode = doc.SelectSingleNode("UpdateInfo/Version"); if (versionNode != null) { newVersion = versionNode.InnerText; } } catch (Exception ex) { LogHelper.WriteError(typeof(VersionManager), ex.ToString()); } return newVersion; } private string GetConfigPath() { string remoteUpdateFolder = ApplicationParameter.UpdateAddress; string configPath = string.Empty; if (remoteUpdateFolder.EndsWith("/")) { configPath = string.Format("{0}UpdateConfig.xml", remoteUpdateFolder); } else { configPath = string.Format("{0}/UpdateConfig.xml", remoteUpdateFolder); } return configPath; } public bool NeedUpdateResource(string memberCode) { bool needUpdateResource = false; if (DateTime.Compare(GetServerModifyTime(memberCode), DateTime.MinValue) > 0) { if (DateTime.Compare(GetServerModifyTime(memberCode), GetLocalModifyTime()) > 0) { needUpdateResource = true; } else { needUpdateResource = false; } } else { needUpdateResource = false; } return needUpdateResource; } private DateTime GetServerModifyTime(string memberCode) { DateTime serverModifyTime = DateTime.MaxValue; try { string configPath = GetServerModifyConfigPath(memberCode); if (configPath != null && configPath != "") { serverXmlDoc.Load(configPath); XmlNode timeNode = serverXmlDoc.SelectSingleNode("UpdateInfo/LastModifyTime"); if (timeNode != null) { string dateTimeString = timeNode.InnerText; if (!DateTime.TryParse(dateTimeString, out serverModifyTime)) { serverModifyTime = DateTime.MinValue; } } } else { serverModifyTime = DateTime.MinValue; } } catch (Exception ex) { serverModifyTime = DateTime.MinValue; LogHelper.WriteError(typeof(VersionManager), ex.ToString()); } return serverModifyTime; } public void SaveXmlToLocal() { string localConfigFolder = Path.Combine(UserManager.SysConfigFolderPath, _folderName); string localConfigFile = Path.Combine(localConfigFolder, "MemberConfig.xml"); try { if (!Directory.Exists(localConfigFolder)) { Directory.CreateDirectory(localConfigFolder); } if (File.Exists(localConfigFile)) { File.Delete(localConfigFile); } serverXmlDoc.Save(localConfigFile); } catch (Exception ex) { throw ex; } } private string GetServerModifyConfigPath(string memberCode) { string memberConfigFolder = ApplicationParameter.UpdateAddress; string configPath = string.Empty; if (memberConfigFolder.EndsWith("/")) { if (RemoteFielExists(string.Format("{0}/MemberConfig.xml", memberConfigFolder + "Config/" + memberCode))) { configPath = string.Format("{0}/MemberConfig.xml", memberConfigFolder + "Config/" + memberCode); _folderName = memberCode; } else if (RemoteFielExists(string.Format("{0}/MemberConfig.xml", memberConfigFolder + "Config/" + ApplicationParameter.UpdateFolderName))) { configPath = string.Format("{0}/MemberConfig.xml", memberConfigFolder + "Config/" + ApplicationParameter.UpdateFolderName); _folderName = ApplicationParameter.UpdateFolderName; } } else { if (RemoteFielExists(string.Format("{0}/MemberConfig.xml", memberConfigFolder + "/Config/" + memberCode))) { configPath = string.Format("{0}/MemberConfig.xml", memberConfigFolder + "/Config/" + memberCode); _folderName = memberCode; } else if (RemoteFielExists(string.Format("{0}/MemberConfig.xml", memberConfigFolder + "/Config/" + ApplicationParameter.UpdateFolderName))) { configPath = string.Format("{0}/MemberConfig.xml", memberConfigFolder + "/Config/" + ApplicationParameter.UpdateFolderName); _folderName = ApplicationParameter.UpdateFolderName; } } return configPath; } private DateTime GetLocalModifyTime() { DateTime localModifyTime = DateTime.MinValue; string localConfigFolder = Path.Combine(UserManager.SysConfigFolderPath, _folderName); string localConfigFile = Path.Combine(localConfigFolder, "MemberConfig.xml"); if (Directory.Exists(localConfigFolder)) { if (File.Exists(localConfigFile)) { XmlDocument doc = new XmlDocument(); doc.Load(localConfigFile); XmlNode timeNode = doc.SelectSingleNode("UpdateInfo/LastModifyTime"); if (timeNode != null) { string dateTimeString = timeNode.InnerText; localModifyTime = DateTime.Parse(dateTimeString); } else { localModifyTime = DateTime.MinValue; } } } else { localModifyTime = DateTime.MinValue; } return localModifyTime; } private bool RemoteFielExists(string remoteFileUri) { bool isExists = false; WebResponse response = null; try { WebRequest request = WebRequest.Create(remoteFileUri); request.Timeout = 3000; response = request.GetResponse(); isExists = response == null ? false : true; } catch (Exception ex) { isExists = false; } finally { if (response != null) { response.Close(); } } return isExists; } } }