123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Xml;
- using System.Text;
- using CB.Common;
- namespace CB.Config
- {
- /// <summary>
- /// 获取基本配置信息
- /// </summary>
- 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;
- }
- /// <summary>
- /// 保存配置文件
- /// </summary>
- /// <param name="config"></param>
- /// <returns></returns>
- public static bool SaveConfig(BaseConfigInfo config)
- {
- var sp = new StringBuilder(2000);
- sp.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
- sp.Append("<Config>\r\n");
- sp.Append(" <AuthorityPageType>" + config.AuthorityPageType + "</AuthorityPageType>\r\n");
- sp.Append(" <TemplateRootPath>" + config.TemplateRootPath + "</TemplateRootPath>\r\n");
- sp.Append(" <TVFilePath>" + config.TVFilePath + "</TVFilePath>\r\n");
- sp.Append(" <TVThumbsPath>" + config.TVThumbsPath + "</TVThumbsPath>\r\n");
- sp.Append(" <URLPrefix>" + config.URLPrefix + "</URLPrefix>\r\n");
- sp.Append(" <SynArticleUrl>" + config.SynArticleUrl + "</SynArticleUrl>\r\n");
- sp.Append(" <SyncCacheServer>" + config.SyncCacheServer + "</SyncCacheServer>\r\n");
- sp.Append("</Config>");
- using (StreamWriter writer = new StreamWriter(configPath, false, System.Text.Encoding.UTF8, sp.Length))
- {
- writer.Write(sp.ToString());
- }
- return true;
- }
- }
- }
|