Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't write into config file

I am using this class for writing into config file. Code is builted and app starts, but every time I check app.config I don't see anything written inside.

What could be the problem?

Here is the code:

public class ConfigSettings
    {
        private ConfigSettings() { }

        public static string ReadSetting(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }

        public static void WriteSetting(string key, string value)
        {
            // load config document for current assembly
            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 RemoveSetting(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()
        {
            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);
            }
        }

        private static string getConfigFilePath()
        {
            return AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();
        }
    }

After that I use this to write into file:

ConfigSettings.WriteSetting("check", "true");
like image 332
user123_456 Avatar asked May 23 '12 11:05

user123_456


People also ask

Can't write into config directory This can usually be fixed by?

Can't write into config directory! This can usually be fixed by giving the webserver write access to the config directory.

Can't write to config file Joomla 3?

Re: Error : Could not write to the configuration file If might be unwritable by Joomla. You can change it to 644 and then Joomla can write to it. Note: Joomla will probably change the config file permissions to something that makes it so you can't overwrite the file using ftp.


2 Answers

You could derive from System.Configuration.ConfigurationSection and make it really simple:

public class MyConfigurationSection : System.Configuration.ConfigurationSection
{
    [ConfigurationProperty("myProperty")]
    public string MyProperty 
    {
        get { return (string)this["myProperty"]; }
        set { this["myProperty"] = value; }
    }
}

Then you add your configSection in your app/web.config.

<configuration>
    <configSections>
        <section name="myConfiguration" type="MyConfigurationSection, MyAssembly" />
    </configSections>

    <myConfiguration myProperty="someValue" />
</configuration>

You can get the instance anywhere like this:

ConfigurationManager.GetSection("myConfiguration") as MyConfigurationSection
like image 162
Ufuk Hacıoğulları Avatar answered Nov 15 '22 00:11

Ufuk Hacıoğulları


check YourExeName.config file instead of app.config

like image 25
Adam Avatar answered Nov 14 '22 23:11

Adam