none
ConfigurationManager for dummies - part 1

    Question

  • Hello,

    after recognizing that the C# example for the ConfigurationManager class on the CD is errorneous, I found a working example here:

    http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

    Starting with this code, I search for a way to update a setting. Using the following code:

    // Get the configuration file.
                System.Configuration.Configuration config =
                  ConfigurationManager.OpenExeConfiguration(
                  ConfigurationUserLevel.None);

                string newKey = "MyKey";

                string newValue = textBox1.Text;

                config.AppSettings.Settings.Add(newKey, newValue);

                // Save the configuration file.
                config.Save(ConfigurationSaveMode.Modified);

    the Add method works as documented - it adds the new value to the existing.

    According to the documentation of the NameValueCollection class, there should be a Set method for replacing the old value. But when I replace Add with Set in the code above, it will not compile.

    It says

    Error 1 "System.Configuration.KeyValueConfigurationCollection" contains no definition for "Set". 

    Why this?


     

    Monday, July 03, 2006 1:55 PM

Answers

  • config.AppSettings.Settings is of type KeyValueConfigurationCollection, and not NameValueCollection. KeyValueConfigurationCollection does not contain a Set method.

    And it looks like its indexer is read-only. So I think you'd have to call Remove to remove the existing value then Add to set the new one.

    Monday, July 03, 2006 2:15 PM
  • No, the documentation is correct. The problem is that you weren't actually using ConfigurationManager.AppSettings. You were using Configuration.AppSettings.Settings.

    If you use ConfigurationManager.AppSettings, you could write this:

    ConfigurationManager.AppSettings.Set(key, val);

    The problem was that your code used config.AppSettings.Settings, which is a different property from ConfigurationManager.AppSettings.

    The static ConfigurationManager.AppSettings property has type NameValueCollection.

    The instance Configuration.AppSettings property has type KeyValueConfigurationCollection.

    Of course the problem is, while ConfigurationManager.AppSettings offers a Set method, it doesn't actually do anything terribly useful, because as far as I can tell, there's no straightforward way to save the settings. (Which is actually a good thing on balance: writing settings out to your App.Config file at runtime is a terrible idea for security reasons - ordinary users won't actually have write access to that file. It's also a terrible idea in multi-user scenarios - even if users do have write access, they'll be overwriting one another's settings...)

    Monday, July 03, 2006 3:04 PM

All replies

  • config.AppSettings.Settings is of type KeyValueConfigurationCollection, and not NameValueCollection. KeyValueConfigurationCollection does not contain a Set method.

    And it looks like its indexer is read-only. So I think you'd have to call Remove to remove the existing value then Add to set the new one.

    Monday, July 03, 2006 2:15 PM
  • Thank you for the fast help!

    This means: both, online and offline documentation of the ConfigurationManager.AppSettings Property is wrong.

     

    Monday, July 03, 2006 2:58 PM
  • No, the documentation is correct. The problem is that you weren't actually using ConfigurationManager.AppSettings. You were using Configuration.AppSettings.Settings.

    If you use ConfigurationManager.AppSettings, you could write this:

    ConfigurationManager.AppSettings.Set(key, val);

    The problem was that your code used config.AppSettings.Settings, which is a different property from ConfigurationManager.AppSettings.

    The static ConfigurationManager.AppSettings property has type NameValueCollection.

    The instance Configuration.AppSettings property has type KeyValueConfigurationCollection.

    Of course the problem is, while ConfigurationManager.AppSettings offers a Set method, it doesn't actually do anything terribly useful, because as far as I can tell, there's no straightforward way to save the settings. (Which is actually a good thing on balance: writing settings out to your App.Config file at runtime is a terrible idea for security reasons - ordinary users won't actually have write access to that file. It's also a terrible idea in multi-user scenarios - even if users do have write access, they'll be overwriting one another's settings...)

    Monday, July 03, 2006 3:04 PM
  • Thank you again for this answer, which of course raises the next question:

    Can I use ConfigurationManager to set user settings?

    I would expect a property ConfigurationManager.UserSetting, but there is none...

    I read there are deprecated classes, but in .net 2.0 I want to use the preferred methods, if possible.

     

    Monday, July 03, 2006 3:53 PM