| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- 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;
- }
- }
- }
|