How to use .NET configuration files (1)

xiaoxiao2021-03-05  28

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 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, 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 . Just returning to the class in the C # is not quite the same, you can refer to the following code example. In addition, if you don't care about the version of the Handler class, you can omit it directly. If NameValueSecionHandler can declare the following:

<

section

Name

= "MyDictionary"

Type

= "System.configuration.nameValueSecionHandler, System" />

Place the declaration section of the above into the configuration file, our configuration structure can be used normally. In the statement, is used to define the name of the section that does not contain configuration data.

Used to define the name of the section containing custom configuration data.
is used to specify the type of defining configuration data. Note that the custom configuration festival cannot be accessed using System.configuration.ConfigurationSettings.AppSettings.get, you want to use System.Configuration.configurationSettings.getConfig. .NET has defined three configuration types: a. NameValueSectionHandler The corresponding 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 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)

转载请注明原文地址:https://www.9cbs.com/read-37404.html

New Post(0)