The .NET application configuration file is used in XML format. Relative to Ini files, its function is strong, and it has strong scalability. Its disadvantage is that it cannot be written directly, that is, the data of the configuration file cannot be modified directly in the program (of course, it is not the scope of this article). The main purpose of this article is to explore how to extend the configuration file and add a variety of custom configuration information.
1. Use
Simple configuration information can be placed directly into the
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, for example, we want to use the following configuration structure to classify the configuration information:
XML Version = "1.0" encoding = "UTF-8"
?>
<
CONFIGURATION
>
Need to join a custom configuration declaration here
->
The following is the content of 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, it is not possible to illuminate such a manner. There is no statement, it is not a custom configuration segment. We must join the statement in front of the configuration file:
The following is a declaration of a custom configuration
->
<
CONFIGSES>
<
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 relationship between declarations and configurations is as follows: It can be seen from the figure that NameValueSectionHandler and DictionarySectionLer are the same in the contents of the defined configuration file, all of which are set by
<
section
Name
= "MyDictionary"
Type
= "System.configuration.nameValueSecionHandler, System" />
Place the
NameValueCollection MyNameValue
=
(NameValueCollection) System.configuration.configurationSettings.getconfig
@ "
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.getconfig
@ "
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.getconfig
@ "
MyConfig / MyInfo
"
);
String
Area
=
MyNameValue [
"
Area
"
];
String
DEVICE
=
MyNameValue [
"
DEVICE
"
];
String
Customer
=
MyNameValue [
"
Customer
"
];
These three types of details can refer to the MSDN documentation. At the same time. Net also defines the IgnoreSectionHandler type, provides a section processing program definition for configuration sections read and processed for systems other than System.Configuration. In addition, .NET provides an IconfigurationSectionHandler interface so that we can also extend themselves to design our own configuration form. (to be continued)