. NET application configuration file, using XML format. Compared to the INI file, it is more powerful than many, and has a strong scalability. The disadvantage is that you cannot write directly, that is, you cannot modify the data of the configuration file directly in your program (not the scope of this article, of course). The main purpose of this article is to explore how to extend a configuration file and add various custom configuration information to it.
. NET application configuration file, using XML format. Compared to the INI file, it is more powerful than many, and has a strong scalability. The disadvantage is that you cannot write directly, that is, you cannot modify the data of the configuration file directly in your program (not the scope of this article, of course). The main purpose of this article is to explore how to extend a configuration file and add various custom configuration information to it.
1. Using <appSettings>
Simple configuration information can be placed directly into the <appSettings> tag. Such as:
<?xml version= "1.0" encoding= "Utf-8"?>
<appSettings>
<add key= "LogFile" value= "D:\log\debug.log"/>
</appSettings>
</configuration>
The corresponding access code is as follows:
String fileName = System.Configuration.ConfigurationSettings.AppSettings.Get ("LogFile");
2. Custom configuration section name
For example, we use the following configuration structure to group configuration information into groups:
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<!--need to add a custom configuration statement here-->
<!--The following are the contents of a 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>
But that's not going to work. We have to add a declaration before the configuration file:
<!--The following are the declarations for custom configurations-->
<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 into the configuration file, our configuration structure can be used normally. The name of a section in a declaration that is used to define,<sectiongroup> without configuration data. <section> is used to define the name of the section that contains the custom configuration data. <section type> is used to specify the type that defines the configuration data ... NET has defined 3 types of configuration:
A. NameValueSectionHandler
The corresponding access code is as follows:
NameValueCollection mynamevalue= (NameValueCollection) System.Configuration.ConfigurationSettings.AppSettings.Get (@ "Myconfig\mynamevalue");
String area = mynamevalue[' area '];
String device= mynamevalue["Device"];
String customer = mynamevalue["Customer"];
B. DictionarySectionHandler
The corresponding access code is as follows:
Hashtable mynamevalue= (Hashtable) System.Configuration.ConfigurationSettings.AppSettings.Get (@ "myconfig\ MyDictionary ");
String area = mynamevalue[' area '];
String device= mynamevalue["Device"];
String customer = mynamevalue["Customer"];
C. SingleTagSectionHandler
The corresponding access code is as follows:
Hashtable mynamevalue= (Hashtable) System.Configuration.ConfigurationSettings.AppSettings.Get (@ "Myconfig\myinfo") ;
String area = mynamevalue[' area '];
String device= mynamevalue["Device"];
String customer = mynamevalue["Customer"];
For more information about these three types, refer to the MSDN documentation. At the same time. NET also defines the Ignoresectionhandler type, which provides a section handler definition for the configuration sections that are read and processed by systems other than System.Configuration.
Besides. NET provides the IConfigurationSectionHandler interface, so that we can also expand ourselves to design our own configuration form.
adjourned