Custom Application configuration file (app. config)

Source: Internet
Author: User
Tags allkeys

1. configuration file Overview:
The application configuration file is a standard XML file, and the XML tag and attribute are case sensitive. It can be changed as needed. developers can use the configuration file to change the settings without re-compiling the application. The root node of the configuration file is configuration. We often access the deleettings, which is a pre-defined configuration section by. Net. The architecture of frequently used configuration files is in the following format. First, I have an impression that I will have a clear understanding through the following examples. The "configuration section" below can be understood as configuring an XML node.

Common configuration file modes:

<Configuration>
<ConfigSections> // The configuration section declaration area, including the configuration section and namespace declaration.
<Section> // configuration section Declaration
<SectionGroup> // defines the configuration section group.
<Section> // configuration section declaration in the configuration section group
<Deleetask> // predefined configuration section
<Custom element for configuration section> // configuration section setting area

2. Only the configuration file and access method of the deleettings section are available.

The following is an example of the most common application configuration file, which only contains the appSettings section.
Program code: [copy the code to the clipboard]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "connectionstring" value = "User ID = sa; Data Source =.; Password =; Initial Catalog = test; Provider = SQLOLEDB.1;"/>
<Add key = "TemplatePATH" value = "Template"/>
</AppSettings>
</Configuration>


Next let's take a look at how such a configuration file works.

Program code: [copy the code to the clipboard]
String _ connectionString = ConfigurationSettings. etettings ["connectionstring"];

You can use the static attribute of the ConfigurationSettings class to directly obtain the configuration information in the configuration file. The type of this attribute is NameValueCollection.

3. Custom configuration file

3.1 custom configuration section

A custom configuration section is divided into two parts in the configuration file: first, declare the configuration section in the <configSections> </configSections> Configuration section ("<section>" in the preceding configuration file mode "), in addition, after <configSections> </configSections>, set the configuration section ("<Custom element for configuration section>" in the preceding configuration file mode). It is similar to a variable that is declared first, and later use the same. The statement for declaring a configuration file is as follows:

<Section name = "" type = ""/>
<Section>: declare the new configuration section to create a new configuration section.

Name: name of the custom configuration section.

Type: type of the custom Configuration section, including System. Configuration. SingleTagSectionHandler, System. Configuration. DictionarySectionHandler, and System. Configuration. NameValueSectionHandler.

Different types not only have different configuration section settings, but also have different operations to access the configuration file. The following is an example of a configuration file that contains the three different types.

Program code:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConfigSections>
<Section name = "Test1" type = "System. Configuration. SingleTagSectionHandler"/>
<Section name = "Test2" type = "System. Configuration. DictionarySectionHandler"/>
<Section name = "Test3" type = "System. Configuration. NameValueSectionHandler"/>
</ConfigSections>

<Test1 setting1 = "Hello" setting2 = "World"/>
<Test2>
<Add key = "Hello" value = "World"/>
</Test2>
<Test3>
<Add key = "Hello" value = "World"/>
</Test3>
</Configuration>


We will describe the above custom configuration section. In the declaration section, use <section name = "Test1" type = "System. Configuration. SingleTagSectionHandler"/> to declare a Configuration section named Test1 and its type is SingleTagSectionHandler. In the Configuration Setting section, use <Test1 setting1 = "Hello" setting2 = "World"/> to set a configuration section. Its first value is Hello, the second value is World. Of course there can be more. The other two configuration sections are similar to this one.
The following describes how to access these custom configuration sections in the program. We used the static method GetConfig of the ConfigurationSettings class to get information about the custom configuration section.

Program code: public static object GetConfig (string sectionName );


The following code accesses these three configuration sections:

Program code:
// Access configuration section Test1
IDictionary IDTest1 = (IDictionary) ConfigurationSettings. GetConfig ("Test1 ");
String str = (string) IDTest1 ["setting1"] + "" + (string) IDTest1 ["setting2"];
MessageBox. Show (str); // output Hello World

// Method 2 for accessing Test1 in configuration section
String [] values1 = new string [IDTest1.Count];
IDTest1.Values. CopyTo (values1, 0 );
MessageBox. Show (values1 [0] + "" + values1 [1]); // output Hello World

// Access configuration section Test2
IDictionary IDTest2 = (IDictionary) ConfigurationSettings. GetConfig ("Test2 ");
String [] keys = new string [IDTest2.Keys. Count];
String [] values = new string [IDTest2.Keys. Count];
IDTest2.Keys. CopyTo (keys, 0 );
IDTest2.Values. CopyTo (values, 0 );
MessageBox. Show (keys [0] + "" + values [0]);

// Access configuration section Test3
NameValueCollection nc = (NameValueCollection) ConfigurationSettings. GetConfig ("Test3 ");
MessageBox. Show (nc. AllKeys [0]. ToString () + "" + nc ["Hello"]); // output Hello World


Through the code above, we can see that different types returned by GetConfig are different, and the specific methods for obtaining configuration content are different.

[Table]
Configuration section handler | return type [br]
[/Table]
SingleTagSectionHandler Systems. Collections. IDictionary

DictionarySectionHandler Systems. Collections. IDictionary

NameValueSectionHandler Systems. Collections. Specialized. NameValueCollection


3.2 custom configuration section group
The configuration section group uses the <sectionGroup> element to group similar configuration sections to the same group. In the configuration section group declaration section, the configuration section inclusion elements are created, the configuration section group is declared in the <configSections> element, and the sections belonging to the group are placed in the <sectionGroup> element. The following is an example of a configuration file containing the configuration section group:

Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConfigSections>
<SectionGroup name = "TestGroup">
<Section name = "Test" type = "System. Configuration. NameValueSectionHandler"/>
</SectionGroup>
</ConfigSections>

<TestGroup>
<Test>
<Add key = "Hello" value = "World"/>
</Test>
</TestGroup>
</Configuration>


The following code accesses the Configuration group:

Program code:
NameValueCollection nc = (NameValueCollection) ConfigurationSettings. GetConfig ("TestGroup/Test ");
MessageBox. Show (nc. AllKeys [0]. ToString () + "" + nc ["Hello"]); // output Hello World

Related Article

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.