Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

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.

 

Posted on Tuesday, November 21, 2006 3:48 PM .Net Framework , Deployment | Back to top


Comments on this post: Update appSettings in App.Config for executable.

# re: Update appSettings in App.Config for executable.
Requesting Gravatar...
Try
http://www.codeproject.com/KB/vb/PersistConnectionStrings.aspx
Left by mhadi on Apr 06, 2009 7:46 PM

# re: Update appSettings in App.Config for executable.
Requesting 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";
Left by Peter on Jan 13, 2011 7:55 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net