views:

6780

answers:

3

When developing a .NET Windows Forms Application we have the choice between those App.config tags to store our configuration values. Which one is better?

<configuration>

  <!-- Choice 1 -->
  <appSettings>
    <add key="RequestTimeoutInMilliseconds" value="10000"/>
  </appSettings>

  <!-- Choice 2 -->
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5612342342" >
        <section name="Project1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5612342342" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <Project1.Properties.Settings>
      <setting name="TABLEA" serializeAs="String">
        <value>TABLEA</value>
      </setting>
    </Project1.Properties.Settings>
  </applicationSettings>

</configuration>
+5  A: 

Application settings can be controlled from a designer (there is usually a Settings.settings file by default) so its easier to modify and you can access them programmatically through the Settings class where they appear like a strongly typed property. You can also have application and user level settings, as well as default settings for rolling back.

This is available from .net 2.0 onwards and deprecates the other way of doing it (as far as I can tell).

More detail is given at: msdn.microsoft.com/en-us/library/k4s6c3a0.aspx

Cookey
+14  A: 

The basic is easier to deal with - just slap in a entry and you're done.

The downside is: there's no type-checking, e.g. you cannot safely assume your number that you wanted to configure there really is a number - someone could put a string into that setting..... you just access it as ConfigurationManager["(key)"] and then it's up to you to know what you're dealing with.

Also, over time, the can get rather convoluted and messy, if lots of parts of your app start putting stuff in there (remember the old windows.ini file? :-)).

If you can, I would prefer and recommend using your own configuration sections - with .NET 2.0, it's really become quite easy, That way, you can a) define your configuration settings in code and have them type-safe and checked, and b) you can cleanly separate YOUR settings from everyone else's. And you can reuse your config code, too!

There's a series of really good articles on you to demystify the .NET 2.0 configuration system on CodeProject:

1) Unraveling the mysteries of .NET 2.0 configuration

2) Decoding the mysteries of .NET 2.0 configuration

3) Cracking the mysteries of .NET 2.0 configuration

Highly recommended! Jon Rista did a great job explaining the configuration system in .NET 2.0.

Cheers, Marc

marc_s
+1  A: 

I like working with the simpler version for storing and accessing single values.

<appSettings>
    <add key="MyConfigKey" value="true"/>
</appSettings>

I wrote a utility class to access values in a typesafe way that allows for default values. If defaults are not provided, then helpful exception messages are given.

You can see/download the class here:

http://www.drewnoakes.com/code/util/app-settings-util/

Drew Noakes
+1, it's simpler especially if you have multiple assemblies (settings typically have a section per assembly). I have a similar helper class. BTW your class currently expects the config file to use culture-sensitive strings which is not a good thing - e.g. should be "Double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out result)" rather than "Double.TryParse(s, out result)". Also to nitpick, the MS coding guidelines recommend GetInt32, GetInt16, GetBoolean rather than GetInt, GetShort, GetBool.
Joe
Thanks Joe, I'll revisit the class and consider your suggestions.
Drew Noakes