TVConfigs.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Text;
  6. namespace CB.TVUCenter.Config
  7. {
  8. public class TVConfigs
  9. {
  10. private static string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\TV.config";
  11. private static long _version = 0;
  12. private static TVConfigInfo _config = null;
  13. private TVConfigs() { }
  14. static TVConfigs()
  15. {
  16. LoadConfig();
  17. }
  18. //加载配置数据
  19. public static void LoadConfig()
  20. {
  21. if (!File.Exists(configPath))
  22. return;
  23. var config = new TVConfigInfo();
  24. XmlDocument doc = new XmlDocument();
  25. doc.Load(configPath);
  26. var root = doc.SelectSingleNode("TVConfig");
  27. if (null != root)
  28. {
  29. var node = root.SelectSingleNode("Debug");
  30. config.Debug = null == node ? false : (node.InnerText.Trim() == "1" ? true : false);
  31. node = root.SelectSingleNode("DecryptKey");
  32. config.DecryptKey = null == node ? "E03F49114203ABCD" : node.InnerText.Trim();
  33. node = root.SelectSingleNode("ExEncryptKey");
  34. config.ExEncryptKey = null == node ? "5pyA5aW955qE55S16KeG6LWw5Yq/5Zu+5Zyo5b2p5ZCn572R" : node.InnerText.Trim();
  35. node = root.SelectSingleNode("TrendChartEncryptKey");
  36. config.TrendChartEncryptKey = null == node ? "5pyA5aW955qE55S16KeG6LWw5Yq/5Zu+5Zyo5b2p5ZCn572R" : node.InnerText.Trim();
  37. node = root.SelectSingleNode("InitImg");
  38. config.InitImg = null == node ? "" : node.InnerText.Trim();
  39. node = root.SelectSingleNode("InitVerticalImg");
  40. config.InitVerticalImg = null == node ? "" : node.InnerText.Trim();
  41. node = root.SelectSingleNode("BackgroundImg");
  42. config.BackgroundImg = null == node ? "" : node.InnerText.Trim();
  43. node = root.SelectSingleNode("BackgroundVerticalImg");
  44. config.BackgroundVerticalImg = null == node ? "" : node.InnerText.Trim();
  45. node = root.SelectSingleNode("ClearOnlineUserTime");
  46. config.ClearOnlineUserTime = null == node ? 5 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim());
  47. node = root.SelectSingleNode("UnLockTime");
  48. config.UnLockTime = null == node ? 120 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim());
  49. node = root.SelectSingleNode("ApiCallTimes");
  50. config.ApiCallTimes = null == node ? 60 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim());
  51. node = root.SelectSingleNode("ApiCallCycle");
  52. config.ApiCallCycle = null == node ? 1 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim());
  53. node = root.SelectSingleNode("AreaVersion");
  54. config.AreaVersion = null == node ? 0 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim());
  55. node = root.SelectSingleNode("ImgVersion");
  56. config.ImgVersion = null == node ? 0 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim());
  57. node = root.SelectSingleNode("ShowByBrowser");
  58. config.ShowByBrowser = null == node ? true : (node.InnerText.Trim() == "1" ? true : false);
  59. }
  60. _version = File.GetLastWriteTime(configPath).Ticks;
  61. _config = config;
  62. }
  63. //获取配置数据
  64. public static TVConfigInfo GetConfig()
  65. {
  66. if (null == _config)
  67. {
  68. LoadConfig();
  69. return _config;
  70. }
  71. if (_version != File.GetLastWriteTime(configPath).Ticks)
  72. LoadConfig();
  73. return _config;
  74. }
  75. public static bool UpdateConfig(TVConfigInfo entity)
  76. {
  77. string str = ResetLoadConfig(entity);
  78. using (StreamWriter writer = new StreamWriter(configPath, false, System.Text.Encoding.UTF8, str.Length))
  79. {
  80. writer.Write(str);
  81. }
  82. return true;
  83. }
  84. private static string ResetLoadConfig(TVConfigInfo entity)
  85. {
  86. StringBuilder sb = new StringBuilder(2000);
  87. sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
  88. sb.Append("<TVConfig>\r\n");
  89. sb.AppendFormat("<Debug>{0}</Debug>\r\n", entity.Debug ? 1 : 0);
  90. sb.AppendFormat("<DecryptKey>{0}</DecryptKey>\r\n", entity.DecryptKey);
  91. sb.AppendFormat("<ExEncryptKey>{0}</ExEncryptKey>\r\n", entity.ExEncryptKey);
  92. sb.AppendFormat("<TrendChartEncryptKey>{0}</TrendChartEncryptKey>\r\n", entity.TrendChartEncryptKey);
  93. sb.AppendFormat(" <InitImg>{0}</InitImg>\r\n", entity.InitImg);
  94. sb.AppendFormat("<InitVerticalImg>{0}</InitVerticalImg>\r\n", entity.InitVerticalImg);
  95. sb.AppendFormat(" <HorizontalImg>{0}</HorizontalImg>\r\n", entity.BackgroundImg);
  96. sb.AppendFormat("<VerticalImg>{0}</VerticalImg>\r\n", entity.BackgroundVerticalImg);
  97. sb.AppendFormat("<ClearOnlineUserTime>{0}</ClearOnlineUserTime>\r\n", entity.ClearOnlineUserTime);
  98. sb.AppendFormat("<UnLockTime>{0}</UnLockTime>\r\n", entity.UnLockTime);
  99. sb.AppendFormat("<ApiCallTimes>{0}</ApiCallTimes>\r\n", entity.ApiCallTimes);
  100. sb.AppendFormat("<ApiCallCycle>{0}</ApiCallCycle>\r\n", entity.ApiCallCycle);
  101. sb.AppendFormat("<AreaVersion>{0}</AreaVersion>\r\n", entity.AreaVersion);
  102. sb.AppendFormat("<ImgVersion>{0}</ImgVersion>\r\n", entity.ImgVersion);
  103. sb.AppendFormat("<ShowByBrowser>{0}</ShowByBrowser>\r\n", entity.ShowByBrowser ? 1 : 0);
  104. sb.Append("</TVConfig>");
  105. return sb.ToString();
  106. }
  107. }
  108. }