using System; using System.Collections.Generic; using System.IO; using System.Xml; using CB.Common; using CB.LotteryCatchData.Entity; namespace CB.LotteryCatchData.Config { public class CatchDataConfigs { private static string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\CatchDataConfig.xml"; private static long _version = 0; private static CatchDataConfigInfo _config = null; private CatchDataConfigs() { } static CatchDataConfigs() { LoadConfig(); } /// /// 获取配置 /// /// public static CatchDataConfigInfo GetConfig() { if (null == _config) { LoadConfig(); return _config; } if (_version != File.GetLastWriteTime(configPath).Ticks) LoadConfig(); return _config; } /// /// 加载配置 /// public static void LoadConfig() { var _file = new FileInfo(configPath); if (!_file.Exists) { throw (new System.IO.FileNotFoundException("未找到数据抓取配置文件!文件路径为:" + configPath)); } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(configPath); XmlElement root = xmlDoc.DocumentElement; var entity = new CatchDataConfigInfo(); XmlNode node = root.SelectSingleNode("Interval"); if (null != node) entity.Interval = Convert.ToDouble(node.InnerText.Trim()); node = root.SelectSingleNode("MaxRequestCount"); if (null != node) entity.MaxRequestCount = TypeConverter.StrToInt(node.InnerText.Trim()); node = root.SelectSingleNode("ThreadCount"); if (null != node) entity.ThreadCount = TypeConverter.StrToInt(node.InnerText.Trim()); node = root.SelectSingleNode("TimeOut"); if (null != node) entity.TimeOut = TypeConverter.StrToInt(node.InnerText.Trim()); var list = root.SelectNodes("LotteryList/Lottery"); if (null != list && 0 < list.Count) { entity.List = new List(); foreach (XmlNode item in list) { var lottery = new LotteryConfigInfo() { DataUrl = item.InnerText.Trim() }; XmlAttribute attr = item.Attributes["Name"]; if (null != attr) lottery.Name = attr.Value.Trim(); attr = item.Attributes["Path"]; if (null != attr) lottery.Path = attr.Value.Trim(); attr = item.Attributes["TimeOut"]; lottery.TimeOut = entity.TimeOut; if (null != attr) lottery.TimeOut = TypeConverter.StrToInt(attr.Value.Trim(), entity.TimeOut); attr = item.Attributes["OpenTimeFomart"]; if (null != attr) lottery.OpenTimeFomart = attr.Value.Trim(); attr = item.Attributes["StartTime"]; if (null != attr) lottery.StartTime = TimeSpan.Parse(attr.Value.Trim()); attr = item.Attributes["OpenPeriod"]; if (null != attr) lottery.OpenPeriod = TypeConverter.StrToInt(attr.Value.Trim()); attr = item.Attributes["EndTime"]; if (null != attr) lottery.EndTime = TimeSpan.Parse(attr.Value.Trim()); attr = item.Attributes["CatchDataProvider"]; if (null != attr) lottery.CatchDataProvider = attr.Value.Trim(); entity.List.Add(lottery); } } _version = File.GetLastWriteTime(configPath).Ticks; _config = entity; } /// /// 保存配置 /// /// public static void SaveConfig() { FileUtil.WriteFile(configPath, _config.ToString()); LoadConfig(); } } }