using System; using System.Collections.Generic; using System.IO; using System.Xml; using System.Text; namespace CB.TVUCenter.Config { public class TVConfigs { private static string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\TV.config"; private static long _version = 0; private static TVConfigInfo _config = null; private TVConfigs() { } static TVConfigs() { LoadConfig(); } //加载配置数据 public static void LoadConfig() { if (!File.Exists(configPath)) return; var config = new TVConfigInfo(); XmlDocument doc = new XmlDocument(); doc.Load(configPath); var root = doc.SelectSingleNode("TVConfig"); if (null != root) { var node = root.SelectSingleNode("Debug"); config.Debug = null == node ? false : (node.InnerText.Trim() == "1" ? true : false); node = root.SelectSingleNode("DecryptKey"); config.DecryptKey = null == node ? "E03F49114203ABCD" : node.InnerText.Trim(); node = root.SelectSingleNode("ExEncryptKey"); config.ExEncryptKey = null == node ? "5pyA5aW955qE55S16KeG6LWw5Yq/5Zu+5Zyo5b2p5ZCn572R" : node.InnerText.Trim(); node = root.SelectSingleNode("TrendChartEncryptKey"); config.TrendChartEncryptKey = null == node ? "5pyA5aW955qE55S16KeG6LWw5Yq/5Zu+5Zyo5b2p5ZCn572R" : node.InnerText.Trim(); node = root.SelectSingleNode("InitImg"); config.InitImg = null == node ? "" : node.InnerText.Trim(); node = root.SelectSingleNode("InitVerticalImg"); config.InitVerticalImg = null == node ? "" : node.InnerText.Trim(); node = root.SelectSingleNode("BackgroundImg"); config.BackgroundImg = null == node ? "" : node.InnerText.Trim(); node = root.SelectSingleNode("BackgroundVerticalImg"); config.BackgroundVerticalImg = null == node ? "" : node.InnerText.Trim(); node = root.SelectSingleNode("ClearOnlineUserTime"); config.ClearOnlineUserTime = null == node ? 5 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim()); node = root.SelectSingleNode("UnLockTime"); config.UnLockTime = null == node ? 120 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim()); node = root.SelectSingleNode("ApiCallTimes"); config.ApiCallTimes = null == node ? 60 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim()); node = root.SelectSingleNode("ApiCallCycle"); config.ApiCallCycle = null == node ? 1 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim()); node = root.SelectSingleNode("AreaVersion"); config.AreaVersion = null == node ? 0 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim()); node = root.SelectSingleNode("ImgVersion"); config.ImgVersion = null == node ? 0 : CB.Common.TypeConverter.StrToInt(node.InnerText.Trim()); node = root.SelectSingleNode("ShowByBrowser"); config.ShowByBrowser = null == node ? true : (node.InnerText.Trim() == "1" ? true : false); } _version = File.GetLastWriteTime(configPath).Ticks; _config = config; } //获取配置数据 public static TVConfigInfo GetConfig() { if (null == _config) { LoadConfig(); return _config; } if (_version != File.GetLastWriteTime(configPath).Ticks) LoadConfig(); return _config; } public static bool UpdateConfig(TVConfigInfo entity) { string str = ResetLoadConfig(entity); using (StreamWriter writer = new StreamWriter(configPath, false, System.Text.Encoding.UTF8, str.Length)) { writer.Write(str); } return true; } private static string ResetLoadConfig(TVConfigInfo entity) { StringBuilder sb = new StringBuilder(2000); sb.Append("\r\n"); sb.Append("\r\n"); sb.AppendFormat("{0}\r\n", entity.Debug ? 1 : 0); sb.AppendFormat("{0}\r\n", entity.DecryptKey); sb.AppendFormat("{0}\r\n", entity.ExEncryptKey); sb.AppendFormat("{0}\r\n", entity.TrendChartEncryptKey); sb.AppendFormat(" {0}\r\n", entity.InitImg); sb.AppendFormat("{0}\r\n", entity.InitVerticalImg); sb.AppendFormat(" {0}\r\n", entity.BackgroundImg); sb.AppendFormat("{0}\r\n", entity.BackgroundVerticalImg); sb.AppendFormat("{0}\r\n", entity.ClearOnlineUserTime); sb.AppendFormat("{0}\r\n", entity.UnLockTime); sb.AppendFormat("{0}\r\n", entity.ApiCallTimes); sb.AppendFormat("{0}\r\n", entity.ApiCallCycle); sb.AppendFormat("{0}\r\n", entity.AreaVersion); sb.AppendFormat("{0}\r\n", entity.ImgVersion); sb.AppendFormat("{0}\r\n", entity.ShowByBrowser ? 1 : 0); sb.Append(""); return sb.ToString(); } } }