VersionManager.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using Muchinfo.PC.Common.Helpers;
  2. using Muchinfo.MTPClient.Infrastructure.Utilities;
  3. using System;
  4. using System.IO;
  5. using System.Net;
  6. using System.Xml;
  7. namespace Muchinfo.MTPClient.Infrastructure.Helpers
  8. {
  9. public class VersionManager
  10. {
  11. readonly XmlDocument serverXmlDoc = new XmlDocument();
  12. private string _folderName = ApplicationParameter.UpdateFolderName;
  13. public string FolderName
  14. {
  15. get
  16. {
  17. return _folderName;
  18. }
  19. }
  20. public string GetServerVersion()
  21. {
  22. string newVersion = string.Empty;
  23. try
  24. {
  25. string configPath = GetConfigPath();
  26. XmlDocument doc = new XmlDocument();
  27. doc.Load(configPath);
  28. XmlNode versionNode = doc.SelectSingleNode("UpdateInfo/Version");
  29. if (versionNode != null)
  30. {
  31. newVersion = versionNode.InnerText;
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. LogHelper.WriteError(typeof(VersionManager), ex.ToString());
  37. }
  38. return newVersion;
  39. }
  40. private string GetConfigPath()
  41. {
  42. string remoteUpdateFolder = ApplicationParameter.UpdateAddress;
  43. string configPath = string.Empty;
  44. if (remoteUpdateFolder.EndsWith("/"))
  45. {
  46. configPath = string.Format("{0}UpdateConfig.xml", remoteUpdateFolder);
  47. }
  48. else
  49. {
  50. configPath = string.Format("{0}/UpdateConfig.xml", remoteUpdateFolder);
  51. }
  52. return configPath;
  53. }
  54. public bool NeedUpdateResource(string memberCode)
  55. {
  56. bool needUpdateResource = false;
  57. if (DateTime.Compare(GetServerModifyTime(memberCode), DateTime.MinValue) > 0)
  58. {
  59. if (DateTime.Compare(GetServerModifyTime(memberCode), GetLocalModifyTime()) > 0)
  60. {
  61. needUpdateResource = true;
  62. }
  63. else
  64. {
  65. needUpdateResource = false;
  66. }
  67. }
  68. else
  69. {
  70. needUpdateResource = false;
  71. }
  72. return needUpdateResource;
  73. }
  74. private DateTime GetServerModifyTime(string memberCode)
  75. {
  76. DateTime serverModifyTime = DateTime.MaxValue;
  77. try
  78. {
  79. string configPath = GetServerModifyConfigPath(memberCode);
  80. if (configPath != null && configPath != "")
  81. {
  82. serverXmlDoc.Load(configPath);
  83. XmlNode timeNode = serverXmlDoc.SelectSingleNode("UpdateInfo/LastModifyTime");
  84. if (timeNode != null)
  85. {
  86. string dateTimeString = timeNode.InnerText;
  87. if (!DateTime.TryParse(dateTimeString, out serverModifyTime))
  88. {
  89. serverModifyTime = DateTime.MinValue;
  90. }
  91. }
  92. }
  93. else
  94. {
  95. serverModifyTime = DateTime.MinValue;
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. serverModifyTime = DateTime.MinValue;
  101. LogHelper.WriteError(typeof(VersionManager), ex.ToString());
  102. }
  103. return serverModifyTime;
  104. }
  105. public void SaveXmlToLocal()
  106. {
  107. string localConfigFolder = Path.Combine(UserManager.SysConfigFolderPath, _folderName);
  108. string localConfigFile = Path.Combine(localConfigFolder, "MemberConfig.xml");
  109. try
  110. {
  111. if (!Directory.Exists(localConfigFolder))
  112. {
  113. Directory.CreateDirectory(localConfigFolder);
  114. }
  115. if (File.Exists(localConfigFile))
  116. {
  117. File.Delete(localConfigFile);
  118. }
  119. serverXmlDoc.Save(localConfigFile);
  120. }
  121. catch (Exception ex)
  122. {
  123. throw ex;
  124. }
  125. }
  126. private string GetServerModifyConfigPath(string memberCode)
  127. {
  128. string memberConfigFolder = ApplicationParameter.UpdateAddress;
  129. string configPath = string.Empty;
  130. if (memberConfigFolder.EndsWith("/"))
  131. {
  132. if (RemoteFielExists(string.Format("{0}/MemberConfig.xml", memberConfigFolder + "Config/" + memberCode)))
  133. {
  134. configPath = string.Format("{0}/MemberConfig.xml", memberConfigFolder + "Config/" + memberCode);
  135. _folderName = memberCode;
  136. }
  137. else if (RemoteFielExists(string.Format("{0}/MemberConfig.xml", memberConfigFolder + "Config/" + ApplicationParameter.UpdateFolderName)))
  138. {
  139. configPath = string.Format("{0}/MemberConfig.xml", memberConfigFolder + "Config/" + ApplicationParameter.UpdateFolderName);
  140. _folderName = ApplicationParameter.UpdateFolderName;
  141. }
  142. }
  143. else
  144. {
  145. if (RemoteFielExists(string.Format("{0}/MemberConfig.xml", memberConfigFolder + "/Config/" + memberCode)))
  146. {
  147. configPath = string.Format("{0}/MemberConfig.xml", memberConfigFolder + "/Config/" + memberCode);
  148. _folderName = memberCode;
  149. }
  150. else if (RemoteFielExists(string.Format("{0}/MemberConfig.xml", memberConfigFolder + "/Config/" + ApplicationParameter.UpdateFolderName)))
  151. {
  152. configPath = string.Format("{0}/MemberConfig.xml", memberConfigFolder + "/Config/" + ApplicationParameter.UpdateFolderName);
  153. _folderName = ApplicationParameter.UpdateFolderName;
  154. }
  155. }
  156. return configPath;
  157. }
  158. private DateTime GetLocalModifyTime()
  159. {
  160. DateTime localModifyTime = DateTime.MinValue;
  161. string localConfigFolder = Path.Combine(UserManager.SysConfigFolderPath, _folderName);
  162. string localConfigFile = Path.Combine(localConfigFolder, "MemberConfig.xml");
  163. if (Directory.Exists(localConfigFolder))
  164. {
  165. if (File.Exists(localConfigFile))
  166. {
  167. XmlDocument doc = new XmlDocument();
  168. doc.Load(localConfigFile);
  169. XmlNode timeNode = doc.SelectSingleNode("UpdateInfo/LastModifyTime");
  170. if (timeNode != null)
  171. {
  172. string dateTimeString = timeNode.InnerText;
  173. localModifyTime = DateTime.Parse(dateTimeString);
  174. }
  175. else
  176. {
  177. localModifyTime = DateTime.MinValue;
  178. }
  179. }
  180. }
  181. else
  182. {
  183. localModifyTime = DateTime.MinValue;
  184. }
  185. return localModifyTime;
  186. }
  187. private bool RemoteFielExists(string remoteFileUri)
  188. {
  189. bool isExists = false;
  190. WebResponse response = null;
  191. try
  192. {
  193. WebRequest request = WebRequest.Create(remoteFileUri);
  194. request.Timeout = 3000;
  195. response = request.GetResponse();
  196. isExists = response == null ? false : true;
  197. }
  198. catch (Exception ex)
  199. {
  200. isExists = false;
  201. }
  202. finally
  203. {
  204. if (response != null)
  205. {
  206. response.Close();
  207. }
  208. }
  209. return isExists;
  210. }
  211. }
  212. }