Using custom configuration settings under the. NET Framework

Source: Internet
Author: User
Tags definition configuration settings key net string version access
The. NET Framework

. NET Framework makes configuration settings very comfortable through an xml-based configuration. It also provides the necessary method to access these settings through the collection class (Collection classes).

The actual configuration data can be accessed through a static ConfigurationSettings class. The class also provides a GetConfig () method that returns an object to an appropriate collection. In this article, I'll demonstrate three ways to access and store configuration information.

The application configuration data is stored in the app.config file and is defined by the configsections node. Each section has a type attribute definition. The 3 types I will discuss here are NameValueSectionHandler, SingleTagSectionHandler and DictionarySectionHandler. You can define a section group with a sectiongroup element. The following is an example of a configuration section definition:

Type= "System.Configuration.NameValueSectionHandler"/>

Recommended to use Type= "system.configuration.namevaluesectionhandler,system,version=1.0.3300.0,culture=neutral, publickeytoken=b77a5c561934e089 "

A section group is a separate configuration section that embeds a sectiongroup element. The following is an example of a section group:

Type= "System.Configuration.NameValueSectionHandler"/>

Finally, the configuration section you specify will be used to construct the custom XML node that stores the configuration data. To add data to a configuration section, simply include the configuration section as an XML node and add collection data to the Add node. The following example is a NameValueSectionHandler configuration section:


The Mycustomsection program segment contains a collection of named values, and its two entries are defined by Key1 and Key2.

SingleTagSectionHandler is more easily constructed. As NameValueSectionHandler, the configuration section can be found in the configsections node. However, in Singletagsectionhandlers and Namevaluesectionhandlers, the configuration data is added differently, as follows:

. . .

Type= "System.Configuration.SingleTagSectionHandler"/>
. . .

. . .

DictionarySectionHandler is similar to NameValueSectionHandler, but the former returns to Hashtable and the latter returns NameValueCollection. When accessing a large number of configuration values, the Hashtable is faster than Namevaluecollectio. DictionarySectionHandler and NameValueSectionHandler are constructed in the same way as in the following example:

. . .

Type= "System.Configuration.DictionarySectionHandler"/>
. . .


. . .

I used it myself and gave an error. It's a very special reason.

System.configuration.dictionarysectionhandler,system,version=1.0.3300.0,culture=neutral, PublicKeyToken= b77a5c561934e089

There is no way to change the type to the above paragraph. It's finally done.

The method of constructing a section group is basically the same as constructing a separate configuration section, except that the custom nodes of the former are nested. To borrow the previous section group definition, the following is the implementation of the section group:






The application configuration settings are accessed by System.Configuration.ConfigurationSettings the GetConfig () method of the namespace and the string values of the custom configuration section, and then the result of the method is converted to the appropriate type.

For SingleTagSectionHandler, the result is converted to the IDictionary interface type of the System.Collections namespace. For NameValueSectionHandler, the result is converted to the NameValueCollection type defined in the System.Collections.Specialized namespace. Finally, for DictionarySectionHandler, the result is converted to the Hashtable type in the System.Collections namespace.

For a section group, the only difference is that the section group name plus the forward slash and configuration section name is passed as a string parameter to the GetConfig () method to access the custom settings.

The following is an instance of using these customizations:

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"]);

The following is a section of the configuration XML code for the above 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>

For more information, please visit the application configuration settings and configuration Schemain the MSDN Library.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.