Maniplate settings of config file in C#.net

How to add/remove settings in config file.

public class ClsConfigSettings

    {

        private ClsConfigSettings() { }

 

        public static string mReadSettings(string key)

        {

            return ConfigurationSettings.AppSettings[key];

        }

 

 

// THIS IS YOUR REQUIRED METHOD

 public static void mWriteSetting(string key, string value)

                        {

      XmlDocument doc = loadConfigDocument();

 

                            // retrieve appSettings node

                            XmlNode node =  doc.SelectSingleNode("//appSettings");

                           

 

                    if (node == null)

                        throw new InvalidOperationException("appSettings section not found in config file.");

 

                    try

                    {

                        // select the 'add' element that contains the key

                        XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));

 

                        if (elem != null)

                        {

                            // add value for key

                            elem.SetAttribute("value", value);

                        }

                        else

                        {

                            // key was not found so create the 'add' element

                            // and set it's key/value attributes

                            elem = doc.CreateElement("add");

                            elem.SetAttribute("key", key);

                            elem.SetAttribute("value", value);

                            node.AppendChild(elem);

                        }

                        doc.Save(getConfigFilePath());

                    }

                    catch

                    {

                        throw;

                    }

                }

 

    public static void mRemoveSetting(string key)

    {

        // load config document for current assembly

        XmlDocument doc = loadConfigDocument();

 

        // retrieve appSettings node

        XmlNode node =  doc.SelectSingleNode("//appSettings");

 

        try

        {

            if (node == null)

                throw new InvalidOperationException("appSettings section not found in config file.");

            else

            {

                // remove 'add' element with coresponding key

                node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']", key)));

                doc.Save(getConfigFilePath());

            }

        }

        catch (NullReferenceException e)

        {

            throw new Exception(string.Format("The key {0} does not exist.", key), e);

        }

    }

 

        private static XmlDocument loadConfigDocument(string Assembly)

        {

            XmlDocument doc = null;

            try

            {

                doc = new XmlDocument();

                doc.Load(Assembly);

                return doc;

            }

            catch (System.IO.FileNotFoundException e)

            {

                throw new Exception("No configuration file found.", e);

            }

        }

   

   private static XmlDocument loadConfigDocument()

    {

        XmlDocument doc = null;

        try

        {

            doc = new XmlDocument();

            doc.Load(getConfigFilePath());

            return doc;

        }

        catch (System.IO.FileNotFoundException e)

        {

            throw new Exception("No configuration file found.", e);

            return null;

        }

        catch (Exception ex)

        {

           

            return null;

        }

    }

 

    private static string getConfigFilePath()

    {

        return Assembly.GetExecutingAssembly().Location + ".config";

    }

}

By Kalit Sikka   Popularity  (1779 Views)