Custom Application configuration file (App. config) (zz)

Source: Internet
Author: User
Tags allkeys

Custom ApplicationProgramConfiguration file (App. config)
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.

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "connectionstring" value = "User ID = sa; da Ta 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.

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.


<? 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.

Public static object getconfig (string sectionname );

The following section accesses the three configuration sections.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 of access configuration section test1
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. Configuration section Handler
Return type

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:

 

<? 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:
Namevaluecollection NC = (namevaluecollection) configurationsettings. getconfig ("testgroup/test ");
MessageBox. Show (nc. allkeys [0]. tostring () + "" + NC ["hello"]); // output Hello World

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.