Using custom configuration settings in .NET
The actual configuration data, however, is accessible through a static ConfigurationSettings class. This class exposes a GetConfig() method that returns an object you can cast into the appropriate Collection. I'll demonstrate three methods for accessing and storing configuration information.
Application configuration data is stored in the accompanying App.config file and is defined in the configSections node. Each section is defined by a type attribute. The three types that I'll discuss are NameValueSectionHandler, SingleTagSectionHandler, and DictionarySectionHandler. You can also define section groups with a sectionGroup node. Here's an example of a section definition:
<section
name="MyCustomSection"
type="System.Configuration.NameValueSectionHandler"/>
Section groups are individual sections nested in a sectionGroup node. Here's an example of a section group:
<sectionGroup
name="CustomGroup">
<section
name="Custom1"
type="System.Configuration.NameValueSectionHandler"/>
<section
name="Custom2"
type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
Finally, the sections that you specify are used to create the custom XML nodes that store the configuration data. To add data to the custom section, you simply include the section as an XML node and use the add node to add Collection data. Here's an example of a NameValueSectionHandler section:
<MyCustomSection>
<add
key="key1" value="value1"/>
<add
key="key2" value="value2"/>
</MyCustomSection>
The MyCustomSection portion contains a name value collection that consists of two entries identified by key1 and key2.
SingleTagSectionHandlers are even easier to construct. The section definition, like the NameValueSectionHandler, is found in the configSections node. But the configuration data is added differently in SingleTagSectionHandlers than it is in NameValueSectionHandlers, as you can see below:
. . .
<section
name="MySingleTagSection"
type="System.Configuration.SingleTagSectionHandler"/>
.
. .
<MySingleTagSection setting1="value1" setting2="value2"
setting3="value3"/>
. . .
DictionarySectionHandlers are very similar to NameValueSectionHandlers except that they return hashtables rather than NameValueCollections. Typically, hashtables are faster than NameValueCollections when accessing a larger number of setting values. DictionarySectionHandlers are constructed the same way as NameValueSectionHandlers. For example:
. . .
<section
name="MyDictionarySection"
type="System.Configuration.DictionarySectionHandler"/>
.
. .
<MyDictionarySection>
<add
key="key1" value="value1"/>
</MyDictionarySection>
. . .
You construct section group settings the same way as the individual sections, except the custom nodes are nested within another custom node. Using the section group definition from earlier, here's an example of the section group implementation:
<CustomGroup>
<Custom1>
<add
key="key1"
value="value1"/>
</Custom1>
<Custom2>
<add
key="key1"
value="value1"/>
</Custom2>
</CustomGroup>
You use the GetConfig() method of the System.Configuration.ConfigurationSettings namespace along with the string value of the custom section to access the application configuration settings. Then, you cast the result of this method to the appropriate type.
For the SingleTagSectionHandlers, cast the result to the IDictionary interface type of the System.Collections namespace. As for NameValueSectionHandlers, cast the result to the NameValueCollection type defined in the System.Collections.Specialized namespace. Finally, for DictionarySectionHandlers, cast the result to the Hashtable type found in the System.Collections namespace.
For section groups, the only difference is to pass the section group name plus a forward slash plus the section name as the string parameter to the GetConfig() method to access the custom setting.
Below is an example that uses each of these custom settings:
System.Collections.IDictionary stsh =
(System.Collections.IDictionary)
System.Configuration.ConfigurationSettings.GetConfig("MySingleTagSection");
System.Collections.Specialized.NameValueCollection nvsh
=
(System.Collections.Specialized.NameValueCollection)
System.Configuration.ConfigurationSettings.GetConfig("MyNameValueSection");
System.Collections.Hashtable
dsh =
(System.Collections.Hashtable)
System.Configuration.ConfigurationSettings.GetConfig("MyDictionarySection");
System.Collections.Specialized.NameValueCollection sgnvsh
=
(System.Collections.Specialized.NameValueCollection)
System.Configuration.ConfigurationSettings.GetConfig("MySectionGroup/MySection
1");
System.Diagnostics.Debug.WriteLine((string)stsh["sample1"]);
System.Diagnostics.Debug.WriteLine((string)nvsh["key1"]);
System.Diagnostics.Debug.WriteLine((string)dsh["key1"]);
System.Diagnostics.Debug.WriteLine((string)sgnvsh["key1"]);
Here's the configuration XML used for the previous code:
<?xml version="1.0" encoding="utf-8"
?>
<configuration>
<configSections>
<section
name="MySingleTagSection"
type="System.Configuration.SingleTagSectionHandler"/>
<section
name="MyDictionarySection"
type="System.Configuration.DictionarySectionHandler"/>
<section
name="MyNameValueSection"
type="System.Configuration.NameValueSectionHandler"/>
<sectionGroup
name="MySectionGroup">
<section
name="MySection1"
type="System.Configuration.NameValueSectionHandler"/>
<section
name="MySection2"
type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<MySingleTagSection
sample1="value1" sample2="value2"
sample3="value3"/>
<MyDictionarySection>
<add
key="key1"
value="value1"/>
<add
key="key2"
value="value2"/>
</MyDictionarySection>
<MyNameValueSection>
<add
key="key1"
value="value1"/>
<add
key="key2"
value="value2"/>
</MyNameValueSection>
<MySectionGroup>
<MySection1>
<add
key="key1"
value="value1"/>
<add
key="key2"
value="value2"/>
</MySection1>
<MySection2>
<add
key="key1"
value="value1"/>
<add
key="key2"
value="value2"/>
</MySection2>
</MySectionGroup>
</configuration>
Visit the MSDN Library for additional information on application configuration settings and the configuration schema.
Phillip Perkins is a contractor with Ajilon Consulting. His experience ranges from machine control and client server to corporate intranet applications.