PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Web/AppConfig.cs

#
C# | 137 lines | 93 code | 11 blank | 33 comment | 6 complexity | d81af443083194a4a7157adf114de615 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. using System;
  2. using System.Configuration;
  3. using System.IO;
  4. using System.Web;
  5. using System.Xml;
  6. namespace BlogEngine.Core.Web
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. public class AppConfig : AppSettingsReader
  12. {
  13. private XmlDocument cfgDoc = new XmlDocument();
  14. public string docName = "web.config";
  15. private XmlNode node = null;
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. /// <returns></returns>
  20. public static AppConfig Instance()
  21. {
  22. return new AppConfig();
  23. }
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. /// <param name="key"></param>
  28. /// <param name="value"></param>
  29. /// <returns></returns>
  30. public bool SetValue(string key, string value)
  31. {
  32. object _lock = new object();
  33. lock (_lock)
  34. {
  35. loadConfigDoc();
  36. // retrieve the appSettings node
  37. node = cfgDoc.SelectSingleNode("//appSettings");
  38. if (node == null)
  39. {
  40. throw new InvalidOperationException("appSettings section not found");
  41. }
  42. try
  43. {
  44. // XPath select setting "add" element that contains this key
  45. XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
  46. if (addElem != null)
  47. {
  48. addElem.SetAttribute("value", value);
  49. }
  50. // not found, so we need to add the element, key and value
  51. else
  52. {
  53. XmlElement entry = cfgDoc.CreateElement("add");
  54. entry.SetAttribute("key", key);
  55. entry.SetAttribute("value", value);
  56. node.AppendChild(entry);
  57. }
  58. //save it
  59. saveConfigDoc(docName);
  60. return true;
  61. }
  62. catch
  63. {
  64. return false;
  65. }
  66. }
  67. }
  68. /// <summary>
  69. ///
  70. /// </summary>
  71. /// <param name="cfgDocPath"></param>
  72. private void saveConfigDoc(string cfgDocPath)
  73. {
  74. try
  75. {
  76. XmlTextWriter writer = new XmlTextWriter(cfgDocPath, null);
  77. writer.Formatting = Formatting.Indented;
  78. cfgDoc.WriteTo(writer);
  79. writer.Flush();
  80. writer.Close();
  81. return;
  82. }
  83. catch (Exception e)
  84. {
  85. throw new FileLoadException("Unable to load the web.config file for modification", e.InnerException);
  86. }
  87. }
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. /// <param name="elementKey"></param>
  92. /// <returns></returns>
  93. public bool removeElement(string elementKey)
  94. {
  95. try
  96. {
  97. loadConfigDoc();
  98. // retrieve the appSettings node
  99. node = cfgDoc.SelectSingleNode("//appSettings");
  100. if (node == null)
  101. {
  102. throw new InvalidOperationException("appSettings section not found");
  103. }
  104. // XPath select setting "add" element that contains this key to remove
  105. node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
  106. saveConfigDoc(docName);
  107. return true;
  108. }
  109. catch
  110. {
  111. return false;
  112. }
  113. }
  114. /// <summary>
  115. ///
  116. /// </summary>
  117. /// <returns></returns>
  118. private void loadConfigDoc()
  119. {
  120. // load the config file
  121. docName = HttpContext.Current.Server.MapPath(docName);
  122. cfgDoc.Load(docName);
  123. return;
  124. }
  125. }
  126. }