.NET How To Create and Use Custom Name-Value Config Sections

Making over-use of the .NET’s AppSettings quickly results in a messy configuration file. Especially when several assemblies’ configurations are combined, it’s quite painful trying to figure out which settings are for which assemblies. Some of the assemblies might even use the same setting keys, causing a real configuration nightmare. Therefore to keep things neat and tidy, without a lot of extra effort, create your own name-value configuration groups and sections, using the sectionGroup and NameValueSectionHandler.

1. Declare a new sectionGroup:

<configSections>
<sectionGroup name=”shinobido.util”>

</sectionGroup>
</configSections>

2. Add a section to the sectionGroup:

<configSections>
<sectionGroup name=”shinobido.util”>
<section name=”shinobido.util.auditLogging” type=”System.Configuration.NameValueSectionHanlder”/>
</sectionGroup>
</configSections>

3. Add the new configuration group and section using the named elements. Inside configuration section “add” elements, with key-value attributes store settings, just like AppSettings.

<shinobido.util>
<shinobido.util.auditLogging>
<add key=”applicationName” value=”BlackSun” />
<add key=”cached” value=”true” />
</shinobido.util.auditLogging>
</shinobido.util>

4. Retrieve the settings using ConfigurationSettings.GetConfig(…), and cast the return value to a NameValueCollection.

ConfigurationSettings.GetConfigCodeSnippet

That’s it. Your own custom config-section in 60 seconds.

Advertisements

3 Comments on “.NET How To Create and Use Custom Name-Value Config Sections”

  1. Reader says:

    Looks very easy..but when I do this, I get messages that “Could not find schema information for the element “add” , “key” and “value”. Also GetSection does not fetch the value for me..

    Thanks.

    • openlandscape says:

      > “Could not find schema information for the element “add” , “key” and “value”
      1. Are you placing these in the correct location in your .config file?
      2. Is your Visual Studio referencing the correct configuration schema?
      3. Which .NET version is your project using?

      > Also GetSection does not fetch the value for me.
      1. This should be GetConfig(…)
      2. What error are you getting?

  2. uwe lesta says:

    Hello,

    Thank you for this helpful sample.
    You have a litle typo in. Thats why reader got an error.
    In 2. Add a section to the sectionGroup:
    change
    System.Configuration.NameValueSectionHanlder
    to
    System.Configuration.NameValueSectionHandler

    regards
    Uwe Lesta


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s