Update appSettings in App.Config for executable.

I wanted programmatically (for administrator) to update configuration settings for .Net 2.0 executable.
The new  in VS 2005 Properties.Settings class saves only user-scope settings, not application scope, which is inconvinient.

I've decided to use The easiest way to read/write AppSettings   from the very good article Read/Write App.config with .NET 2.0/Enterprise Library   However I noticed two issues:

 config.AppSettings.Settings (KeyValueConfigurationCollection class) has Add and Remove methods, but does not have Set method, so I had to create static helper function:
        public static void Set(KeyValueConfigurationCollection settings, string key, string value)
        {

            if (settings[key] != null)

            {

                settings.Remove(key);

            }

            settings.Add(key, value);

        }

 

Also if the appSettings element uses the File attribute,

config.Save() for executable updates the main file, not specified in File attribute "appSettings" section.So if the value is stored in the referenced file, saving "appSettings" to the main file has no effect.

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Tuesday, November 21, 2006 3:48 PM
Print

Comments on this entry:

# re: Update appSettings in App.Config for executable.

Left by mhadi at 4/6/2009 7:46 PM
Gravatar
Try
http://www.codeproject.com/KB/vb/PersistConnectionStrings.aspx

# re: Update appSettings in App.Config for executable.

Left by Peter at 1/13/2011 7:55 AM
Gravatar
I'm not sure you really need to remove and add a setting in order to update its value, as you've done in your helper method. You can just update it directly like this:

config.AppSettings.Settings["Key Name"].Value = "New Value";

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910