using System; using System.Collections.Generic; using System.IO; using System.Xml; using System.Text; using CB.Common; namespace CB.Config { /// /// 获取基本配置信息 /// public class BaseConfigs { private static string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\BaseConfigs.config"; private static long version = 0; private static BaseConfigInfo config = null; private static object lockObject = new object(); static BaseConfigs() { LoadConfig(); } private BaseConfigs() { } private static void LoadConfig() { var _config = new BaseConfigInfo(); if (File.Exists(configPath)) { XmlDocument doc = new XmlDocument(); doc.Load(configPath); var root = doc.SelectSingleNode("Config"); if (null != root) { var node = root.SelectSingleNode("AuthorityPageType"); if (null != node) _config.AuthorityPageType = TypeConverter.StrToInt(node.InnerText.Trim()); node = root.SelectSingleNode("TemplateRootPath"); if (null != node) _config.TemplateRootPath = node.InnerText.Trim(); node = root.SelectSingleNode("TVFilePath"); if (null != node) _config.TVFilePath = node.InnerText.Trim(); node = root.SelectSingleNode("TVThumbsPath"); if (null != node) _config.TVThumbsPath = node.InnerText.Trim(); node = root.SelectSingleNode("URLPrefix"); if (null != node) _config.URLPrefix = node.InnerText.Trim(); node = root.SelectSingleNode("SynArticleUrl"); if (null != node) _config.SynArticleUrl = node.InnerText.Trim(); node = root.SelectSingleNode("SyncCacheServer"); if (null != node) _config.SyncCacheServer = node.InnerText.Trim(); } version = File.GetLastWriteTime(configPath).Ticks; } config = _config; } public static BaseConfigInfo GetConfig() { if (null == config) { LoadConfig(); } if (version != File.GetLastWriteTime(configPath).Ticks) { LoadConfig(); } return config; } /// /// 保存配置文件 /// /// /// public static bool SaveConfig(BaseConfigInfo config) { var sp = new StringBuilder(2000); sp.Append("\r\n"); sp.Append("\r\n"); sp.Append(" " + config.AuthorityPageType + "\r\n"); sp.Append(" " + config.TemplateRootPath + "\r\n"); sp.Append(" " + config.TVFilePath + "\r\n"); sp.Append(" " + config.TVThumbsPath + "\r\n"); sp.Append(" " + config.URLPrefix + "\r\n"); sp.Append(" " + config.SynArticleUrl + "\r\n"); sp.Append(" " + config.SyncCacheServer + "\r\n"); sp.Append(""); using (StreamWriter writer = new StreamWriter(configPath, false, System.Text.Encoding.UTF8, sp.Length)) { writer.Write(sp.ToString()); } return true; } } }