Tech

Guides
 

Use custom configuration settings in .NET

By Philip Perkins, TechRepublic
Wednesday, October 19, 2005 02:54 PM
Here are three methods for accessing and storing configuration information in the .Net Framework.

The .NET Framework makes configuration settings a breeze with XML-based configurations. It also supplies the means necessary for accessing these settings through Collection classes.

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.



WORTHWHILE?

0

0 votes
Blog

Talkback 0 comments

There are currently no comments for this post.


Guest user

Guest user

Level: 
Joined: —
Already a member? Log in »



 

Loading...

Whitepapers / Case Studies

Downloads

Web Development News


Tech Jobs Now!

Tags

  1. advertisement
  2. apple iphone
  3. blog
  4. china
  5. google inc.
  6. industry
  7. information technology
  8. internet
  9. microsoft corp.
  10. mobile
  11. network
  12. phone
  13. revenue
  14. security
  15. software
  16. strategy
  17. team
  18. u.s.
  19. web
  20. yahoo! inc.