PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

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