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.

This article is part of the GWB Archives. Original Author: Michael Freidgeim

New on Geeks with Blogs