BaseConfigs.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Text;
  6. using CB.Common;
  7. namespace CB.Config
  8. {
  9. /// <summary>
  10. /// 获取基本配置信息
  11. /// </summary>
  12. public class BaseConfigs
  13. {
  14. private static string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\BaseConfigs.config";
  15. private static long version = 0;
  16. private static BaseConfigInfo config = null;
  17. private static object lockObject = new object();
  18. static BaseConfigs()
  19. { LoadConfig(); }
  20. private BaseConfigs() { }
  21. private static void LoadConfig()
  22. {
  23. var _config = new BaseConfigInfo();
  24. if (File.Exists(configPath))
  25. {
  26. XmlDocument doc = new XmlDocument();
  27. doc.Load(configPath);
  28. var root = doc.SelectSingleNode("Config");
  29. if (null != root)
  30. {
  31. var node = root.SelectSingleNode("AuthorityPageType");
  32. if (null != node)
  33. _config.AuthorityPageType = TypeConverter.StrToInt(node.InnerText.Trim());
  34. node = root.SelectSingleNode("TemplateRootPath");
  35. if (null != node)
  36. _config.TemplateRootPath = node.InnerText.Trim();
  37. node = root.SelectSingleNode("TVFilePath");
  38. if (null != node)
  39. _config.TVFilePath = node.InnerText.Trim();
  40. node = root.SelectSingleNode("TVThumbsPath");
  41. if (null != node)
  42. _config.TVThumbsPath = node.InnerText.Trim();
  43. node = root.SelectSingleNode("URLPrefix");
  44. if (null != node)
  45. _config.URLPrefix = node.InnerText.Trim();
  46. node = root.SelectSingleNode("SynArticleUrl");
  47. if (null != node)
  48. _config.SynArticleUrl = node.InnerText.Trim();
  49. node = root.SelectSingleNode("SyncCacheServer");
  50. if (null != node)
  51. _config.SyncCacheServer = node.InnerText.Trim();
  52. }
  53. version = File.GetLastWriteTime(configPath).Ticks;
  54. }
  55. config = _config;
  56. }
  57. public static BaseConfigInfo GetConfig()
  58. {
  59. if (null == config)
  60. { LoadConfig(); }
  61. if (version != File.GetLastWriteTime(configPath).Ticks)
  62. { LoadConfig(); }
  63. return config;
  64. }
  65. /// <summary>
  66. /// 保存配置文件
  67. /// </summary>
  68. /// <param name="config"></param>
  69. /// <returns></returns>
  70. public static bool SaveConfig(BaseConfigInfo config)
  71. {
  72. var sp = new StringBuilder(2000);
  73. sp.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
  74. sp.Append("<Config>\r\n");
  75. sp.Append(" <AuthorityPageType>" + config.AuthorityPageType + "</AuthorityPageType>\r\n");
  76. sp.Append(" <TemplateRootPath>" + config.TemplateRootPath + "</TemplateRootPath>\r\n");
  77. sp.Append(" <TVFilePath>" + config.TVFilePath + "</TVFilePath>\r\n");
  78. sp.Append(" <TVThumbsPath>" + config.TVThumbsPath + "</TVThumbsPath>\r\n");
  79. sp.Append(" <URLPrefix>" + config.URLPrefix + "</URLPrefix>\r\n");
  80. sp.Append(" <SynArticleUrl>" + config.SynArticleUrl + "</SynArticleUrl>\r\n");
  81. sp.Append(" <SyncCacheServer>" + config.SyncCacheServer + "</SyncCacheServer>\r\n");
  82. sp.Append("</Config>");
  83. using (StreamWriter writer = new StreamWriter(configPath, false, System.Text.Encoding.UTF8, sp.Length))
  84. {
  85. writer.Write(sp.ToString());
  86. }
  87. return true;
  88. }
  89. }
  90. }