Describes how to use the. NET configuration file

Source: Internet
Author: User
. Net Applications Program Configuration File in XML format. Compared with the INI file, it has a lot of functions and strong scalability. Its disadvantage is that it cannot directly perform write operations, that is, it cannot directly modify the data in the configuration file in the Program (of course, it does not mean no, but it is not the scope discussed in this article ). This article mainly aims to explore how to expand the configuration file and add various custom configuration information to it.

1. Use
Simple configuration information can be directly put into the tag. For example:
<XML version = "1.0" encoding = "UTF-8"?>
<Deleetask>
<Add key = "logfile" value = "D: \ log \ Debug. log"/>
Deleetask>
</Configuration>
Access Code As follows:
String filename = system. configuration. configurationsettings. deleettings. Get ("logfile ");
2. Custom configuration section name
For example, we need to use the following configuration structure to classify the configuration information into groups:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Myconfig>
<Mydictionary>
<Add key = "area" value = "Fuzhou"/>
<Add key = "device" value = "Printer"/>
<Add key = "customer" value = "MUF"/>
</Mydictionary>
<Mynamevalue>
<Add key = "area" value = "Fuzhou"/>
<Add key = "device" value = "Printer"/>
<Add key = "customer" value = "MUF"/>
</Mynamevalue>
<Myinfo
Area = "Fuzhou" device = "Printer" customer = "MUF"
/>
</Myconfig>
</Configuration>
However, this alone cannot be used. The custom configuration segment cannot be used without declaration. We must add a declaration before the configuration file:
<Configsections>
<Sectiongroup name = "myconfig">
<Section name = "mydictionary"
Type = "system. configuration. namevaluesectionhandler, system, version = 1.0.3300.0, culture = neutral, publickeytoken = b77a5c561934e089"
/>
<Section name = "mynamevalue"
Type = "system. configuration. dictionarysectionhandler, system, version = 1.0.3300.0, culture = neutral, publickeytoken = b77a5c561934e089"
/>
<Section name = "myinfo"
Type = "system. configuration. singletagsectionhandler, system, version = 1.0.3300.0, culture = neutral, publickeytoken = b77a5c561934e089"
/>
</Sectiongroup>
</Configsections>
The description and configuration relationship are as follows:

As shown in the figure, namevaluesectionhandler and dictionarysectionhandler have the same content in the definition configuration file and are used to set the content. The classes returned to C # are not the same. You can refer to the following code example.
In addition, if you do not care about the version of the handler class, you can omit it directly. For example, namevaluesectionhandler can directly declare as follows:
<Section name = "mydictionary" type = "system. configuration. namevaluesectionhandler, system"/> put the preceding Declaration section in the configuration file, and our configuration structure can be used normally. Declare to define the name of a section without configuration data. Defines the name of a section containing custom configuration data. Specifies the type of configuration data .. Net has defined three configuration types:
A. namevaluesectionhandler
The "access code" is as follows: namevaluecollection mynamevalue = ");
String area = mynamevalue ["area"];
String string device = mynamevalue ["device"];
String = mynamevalue ["customer"];
B. dictionarysectionhandler
The access code is as follows:
Hashtable mynamevalue = (hashtable) system. configuration. configurationsettings. deleettings. Get (@ "myconfig \ mydictionary ");
String area = mynamevalue ["area"];
String device = mynamevalue ["device"];
String customer = mynamevalue ["customer"];
C. singletagsectionhandler
The access code is as follows:
Hashtable mynamevalue = (hashtable) system. configuration. configurationsettings. deleettings. Get (@ "myconfig \ myinfo ");
String area = mynamevalue ["area"];
String device = mynamevalue ["device"];
String customer = mynamevalue ["customer"];

For more information about the three types, see the msdn documentation. At the same time,. Net also defines the ignoresectionhandler type, which provides the section handler definition for the configuration section read and processed by systems other than system. configuration.
In addition,. NET provides the iconfigurationsectionhandler interface, so that we can expand it on our own to design our own configuration form.
3. Custom configuration structure (using iconfigurationsectionhandler)
If the following configuration information can be repeated multiple times in myinfo, how can I read the configuration? In this case, you need to use the custom configuration program. <Myconfigs>
<Myinfo area = "Fuzhou" device = "Printer" customer = "MUF"/>
<Myinfo area = "Shanghai" device = "mobile" customer = "Liny"/>
</Myconfig>
The access code is as follows:
Hashtable foreign table = (hashtable) configurationsettings. getconfig ("myconfigs ");
Debug. Assert (partition table. Count = 2 );
Hashtable writable Fuzhou = (hashtable) partitioned table ["Fuzhou"];
Hashtable mongoshanghai = (hashtable) mongotable ["Shanghai"];
Debug. Assert (cfgfuzhou ["device"] = "Printer ");
Debug. Assert (mongoshanghai ["device"] = "mobile ");
Debug. Assert (export Fuzhou ["customer"] = "MUF ");
Debug. Assert (mongoshanghai ["customer"] = "Liny ");
Foreach (hashtable cfg in customized table. values)
{
Console. writeline ("Area = {0} device = {1} customer = {2}", CFG ["area"], CFG ["device"], CFG ["customer"]);
}
To use the preceding access code to access the configuration structure, we need to generate a specific configuration read class (configurationsectionhandler). The example is very simple and will not be described as follows:
Public
Class myinfosectionhandler: iconfigurationsectionhandler
{
Public
Object create (Object parent, object configcontext, system. xml. xmlnode Section)
{
Hashtable Config =
New hashtable ();
Foreach (xmlnode node in section. childnodes)
{
If (node. Name! =
"Myinfo ")
Throw
New system. configuration. configurationexception ("Unrecognized configuration items", node );

Hashtable item =
New hashtable ();
Foreach (xmlattribute ATTR in node. attributes)
{
Switch (ATTR. Name)
{
Case
"Area ":
Case
"Device ":
Case
"Customer ":

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.