Application Settings

Configuration files also allow the programmer to define settings that can be read from within an application. The System.Configuration namespace contains the classes that provide access to these settings.

Simple Settings

The <appSettings> section of a configuration file can be used to define key/value pairs, as shown in the following example:

<configuration>
    <appSettings>
        <add key="MyFirstKey" value="MyFirstValue"/>
        <add key="MySecondKey" value="MySecondValue"/>
        <add key="MyThirdKey" value="MyThirdValue"/>
    </appSettings>
</configuration>

These key/value pairs are accessible independently or as a collection through the AppSettings property of the System.Configuration.ConfigurationSettings class, as shown in the following example:

// Access values independantly
Console.WriteLine("First Value: " +
    ConfigurationSettings.AppSettings["MyFirstKey"]);
Console.WriteLine("Second Value: " +
    ConfigurationSettings.AppSettings["MySecondKey"]);
// Access key/values as a collection
System.Collections.Specialized.NameValueCollection x_coll =
    ConfigurationSettings.AppSettings;

foreach (string x_key in x_coll.AllKeys) {
    Console.WriteLine("KEY {0}, Value {1}",
        x_key, x_coll[x_key]);
}

The AppSettings property returns an instance of System.Collections.Specialized.NameValueCollection, which can be used to iterate through the keys and values or to access a key by name or index. See Chapter 9, for more information about the NameValueCollection class. The preceding example writes out the following statements:

First Value: MyFirstValue
Second Value: MySecondValue
KEY MyFirstKey, Value MyFirstValue
KEY MySecondKey, Value MySecondValue
KEY MyThirdKey, Value MyThirdValue

Complex Settings

Configuration files allow the programmer to specify more complex settings, grouped into sections. Three types of section can be used, but all must be declared in the same manner. The declaration of a configuration section includes the section name and the type from the System.Configuration namespace that will be used to parse the section.

Declarations of sections are included in the configSections configuration file element, but the contents of the section are declared as an element of the configuration element, as shown here:

<configuration>
    <configSections>
        <section name="sectionname" type="section parser"/>
    </configSections>

    <!-- specification of section values -->
</configuration>

The section values are accessible through the GetConfig method of the System.Configuration.ConfigurationSettings class; the method takes the name of the section as an argument and returns an Object that can be cast to a collection type in order to access the keys and values.

Single Tag Sections

Single tag sections allow key/value pairs to be defined as the attributes of an XML element. The following example demonstrates the declaration and specification of a single tag section:

<configuration>
    <configSections>
        <section name="MySingleSection"
            type="System.Configuration.SingleTagSectionHandler"/>
    </configSections>

    <MySingleSection
        MyFirstKey="MyFirstValue"
        MySecondKey="MySecondValue"
        MyThirdKey="MyThirdValue"/>

</configuration>

The following code fragment demonstrates how to access the values in a single tag section:

IDictionary x_dict =
    (IDictionary)ConfigurationSettings.GetConfig(
        "MySingleSection");

Console.WriteLine("First Value: "  + x_dict["MyFirstKey"]);
Console.WriteLine("Second Value: " + x_dict["MySecondKey"]);

foreach (string x_key in x_dict.Keys) {
    Console.WriteLine("KEY {0}, Value {1}",
        x_key, x_dict[x_key]);
}

Name/Value Sections

Name/value sections are defined in the same way as the simple application settings just shown. The following configuration file demonstrates this type of section:

<configuration>
    <configSections>
        <section name="nameValueSection"
            type="System.Configuration.NameValueSectionHandler"/>
    </configSections>

    <nameValueSection>
        <add key="MyFirstKey"    value="MyFirstValue"/>
        <add key="MySecondKey"   value="MySecondValue"/>
        <add key="MyThirdKey"    value="MyThirdValue"/>
    </nameValueSection>
</configuration>

When accessing name/value sections, the GetConfig method returns an instance of the System.Collections.Specialized.NameValueCollection class. The following code fragment demonstrates the use of a name/value section:

NameValueCollection x_dict = (NameValueCollection)
    ConfigurationSettings.GetConfig("nameValueSection");

Console.WriteLine("First Value: "  + x_dict["MyFirstKey"]);
Console.WriteLine("Second Value: " + x_dict["MySecondKey"]);

foreach (string x_key in x_dict.Keys) {
    Console.WriteLine("KEY {0}, Value {1}",
        x_key, x_dict[x_key]);
}

Dictionary Sections

Dictionary sections are declared in the same way as name/value sections, but the return type from the GetConfig method is a System.Collections.Hashtable. The following configuration file demonstrates a dictionary section:

<configuration>
    <configSections>
        <section name="nameValueSection"
            type="System.Configuration.DictionarySectionHandler"
            />
    </configSections>

    <nameValueSection>
        <add key="MyFirstKey"   value="MyFirstValue"/>
        <add key="MySecondKey"  value="MySecondValue"/>
        <add key="MyThirdKey"   value="MyThirdValue"/>
    </nameValueSection>
</configuration>

Ignore Sections

Ignore sections are included in the configuration file system to allow sections of a file to be skipped by the parser. This is useful if a configuration file contains information that will be processed by another system.

The following section demonstrates an ignore section:

<configuration>
    <configSections>
        <section name="nameValueSection"
            type="System.Configuration.IgnoreSectionHandler"/>
    </configSections>

    <nameValueSection>
        <add key="MyFirstKey"   value="MyFirstValue"/>
        <add key="MySecondKey"  value="MySecondValue"/>
        <add key="MyThirdKey"   value="MyThirdValue"/>
    </nameValueSection>
</configuration>

Declaring the type to be System.Configuration.IgnoreSectionHandler (shown in boldface) causes calls to the ConfigurationSettings.GetContent method to always return null, irrespective of the section contents.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset