. NET Configuration File Usage

Source: Internet
Author: User

. NET application 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 <deleettings>
Simple configuration information can be directly placed in the <deleetask> tag. For example:

 

Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<Deleetask>
<Add key = "LogFile" value = "d: \ log \ debug. log"/>
</AppSettings>
</Configuration>

 

The access code is as follows:

 

Code
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:

 

Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<! -- Add a custom configuration declaration here -->
<! -- The following is the content of the custom 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. We must add a declaration before the configuration file:

 

Code
<! -- The following is a custom configuration declaration -->
<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>

Put this 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 .. NET has defined three configuration types:
A. NameValueSectionHandler
The access code is as follows:

 

Code
NameValueCollection myNameValue = (NameValueCollection) System. Configuration. ConfigurationSettings. deleettings. Get (@ "myConfig \ myNameValue ");
String Area = myNameValue ["Area"];
String Device = myNameValue ["Device"];
String Customer = myNameValue ["Customer"];

B. DictionarySectionHandler
The access code is as follows:

 

Code
Hashtable myNameValue = (Hashtable) System. Configuration. ConfigurationSettings. deleettings. Get (@ "myConfig \ myDictionary ");
String Area = myNameValue ["Area"];
String Device = myNameValue ["Device"];
String Customer = myNameValue ["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.