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; 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.
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>: Can I create a new configuration section by declaring a new configuration section? /P>
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 code accesses these three configuration sections:
// 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. 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