Partilhar via


Changing application settings programmatically in .NET Framework 2.0

As I promised to provide a component to enable you to change the values of the application-scope settings in .NET Framework 2.0, I'm now providing it.

It's a very simple dll written by C#, you can find the Source code here.

To use this dll you will need to get instance from the SettingWriter class as following

ApplicationSettingsWriter.SettingsWriter writer = new ApplicationSettingsWriter.SettingsWriter();

Then use the indexes of this object to change the value of the settings like this

writer["sampleSettings"] = "Omar";

In this example, "sampleSettings" is the key of the setting in the app.config. In Visual Studio 2005, you can access your settings visual by opening the properties window of the project (see figure below).

Writing the previous line indeed changes the value of the app.config but if you tried this line to retrieve the value, you wouldn't get your change

MessageBox.Show(Properties.Settings.Default.sampleSettings);

That's because .NET Framework loads the application settings with the loading of the application domain and caches them for performance. If you need to update this cache, you need to call the Reloud() method as following

Properties.Settings.Default.Reload();

You can download the binary from here and the source code from here.

Best regards,

Mohamed

Comments

  • Anonymous
    May 09, 2007
    I need to change the application setting for an ASP.net application(webservice actually) but I dunno how could u help please? thanx Enas

  • Anonymous
    June 06, 2007
    why we need to do that, while we have a user scope for the settings

  • Anonymous
    July 13, 2007
    Simple, some application need to operate with app settings that span user level settings i.e. global

  • Anonymous
    October 31, 2007
    Wicked! That's what I've been looking for. I've made some changes, though: public class ApplicationSettingsWriter {  public string this[string key]  {    set    {      lock(this)      {        XmlDocument doc = new XmlDocument();        doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);        XmlNode settingsNode = doc.GetElementsByTagName("applicationSettings")[0];        XmlNode node = settingsNode.SelectSingleNode(typeof(Settings).FullName + "/setting[@name='" + key + "']");        if(node != null)        {          XmlNode valueNode = node.SelectSingleNode("value");          if(valueNode != null)          {            valueNode.FirstChild.Value = value;          }        }        doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);      }      Settings.Default.Reload();    }  } } Cheers!

  • Anonymous
    October 31, 2007
    I just forgot to mention that you have to remember that if you install the application (actually its config file) to a drive with NTFS (which usually is the Program Files folder) then you must get sure that a user which wants to change tha application settings through this writter class must have read/WRITE rights to that folder and the configuration file itself (appname.exe.config). Thanks again!

  • Anonymous
    May 22, 2008
    there is a flaw in the code. one should note that the settings need A VALUE before you can save. if any settings are blank, it wont work. the problem lies here....        private void updateConfigurationFile(string key, string newValue)        {            lock (this)            {                XmlDocument doc = new XmlDocument();                doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);                XmlNode settingsNode = doc.GetElementsByTagName("applicationSettings")[0];                string applicationName = AppDomain.CurrentDomain.FriendlyName.Split('.')[0];                XmlNode node = settingsNode.SelectSingleNode(String.Format(applicationName + ".Properties.Settings/setting[@name='{0}']", key));                if (node != null)                {                    XmlNode valueNode = null;                    valueNode=node.SelectSingleNode("value"); //PROBEM HERE                    if (valueNode != null)                    {                        valueNode.FirstChild.Value = newValue;                    }                }                doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);            }        } to reiterate the problem is here //PROBEM HERE if (valueNode != null) {                        valueNode.FirstChild.Value = newValue; } I added a little code if (valueNode != null) { //first check for child nodes if (valueNode.HasChildNodes == true) {  valueNode.FirstChild.Value = newValue; } else { //now code would need to go here to ADD a child node. I know it starts off like this: valueNode.AppendChild... //but I'm not sure how to finish it. } }