How to use the. NET configuration file (1)

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.

How to use the. NET configuration file (I) How to Use the. NET configuration file (ii) Mu Feng wangzhi

1. Use <deleettings>
Simple configuration information can be directly placed in the <deleetask> tag. For example: <? XML version = "1.0" encoding = "UTF-8" ?>
< Appsettings >
  < Add Key = "Logfile" Value = "D: \ log \ Debug. log" />
</ Appsettings >
</ Configuration >

AccessCodeAs follows:

String Filename = System. configuration. configurationsettings. receivettings. 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 >
<! -- You need to add a custom configuration declaration here -->
<! -- The content of custom configuration is as follows: -->
< 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:

<! -- The following is a custom configuration statement. -->
< 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, both of which use <add> 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 <configsections> declaration section in the configuration file, and our configuration structure can be used normally. Declaration, <sectiongroup> is used to define the name of a section without configuration data. <Section> defines the name of a section containing custom configuration data. <Section type> specifies the type of configuration data.

Note that system. configuration. configurationsettings.Deleetask. getSystem. configuration. configurationsettings.Getconfig.

. Net has defined three configuration types:
A. namevaluesectionhandler
The access code is as follows:

Namevaluecollection mynamevalue = (Namevaluecollection) system. configuration. configurationsettings.Getconfig( @" Myconfig/mynamevalue " );
String Area = Mynamevalue [ " Area " ];
String Device = Mynamevalue [ " Device " ];
String Customer = Mynamevalue [ " Customer " ];

B. dictionarysectionhandler
The access code is as follows:

Hashtable mynamevalue = (Hashtable) system. configuration. configurationsettings.Getconfig( @" 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.Getconfig( @" 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.

(To be continued)

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.