. 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"];