Configuration file (App. config file), configuration file app. config

Source: Internet
Author: User

Configuration file (App. config file), configuration file app. config

1. configuration file Overview:
The application configuration file is a standard XML file, and the XML tag and attribute are case sensitive. It can be changed as needed. developers can use the configuration file to change the settings without re-compiling the application. The configuration file's root node is
Configuration. We often access the deleettings, which is a pre-defined configuration section by. Net. The architecture of frequently used configuration files is in the following format. First, I have an impression that I will have a clear understanding through the following examples. The following"
The configuration section is used to configure an XML node.

Common configuration file modes:

<Configuration>
<ConfigSections> // The configuration section declaration area, including the configuration section and namespace declaration.
<Section> // configuration section Declaration
<SectionGroup> // defines the configuration section group.
<Section> // configuration section declaration in the configuration section group
<Deleetask> // predefined configuration section
<Custom element for configuration section> // configuration section setting area

2. Only the configuration file and access method of the deleettings section are available.

The following is an example of the most common application configuration file, which only contains the appSettings section.

Program code:

<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="connectionstring" value="User ID=sa;Data Source=.;Password=;Initial Catalog=test;Provider=SQLOLEDB.1;" /> <add key="TemplatePATH" value="Template" /> </appSettings> </configuration> 


Next let's take a look at how such a configuration file works.

Program code:
String _ connectionString = ConfigurationSettings. etettings ["connectionstring"];

You can use the static attribute of the ConfigurationSettings class to directly obtain the configuration information in the configuration file. The type of this attribute is NameValueCollection.


3. Custom configuration file

3.1 custom configuration section

A custom configuration section is divided into two parts in the configuration file: first, declare the configuration section in the <configSections> </configSections> Configuration section ("<section>" in the above configuration file mode), and in the <configSections> </
ConfigSections> then set the configuration section ("<Custom element for configuration section>" in the above configuration file mode). It is similar to a variable that is declared first and then used later. The statement for declaring a configuration file is as follows:

<Section name = "" type = ""/>
<Section>: declare the new configuration section to create a new configuration section.

Name: name of the custom configuration section.

Type: type of the custom Configuration section, including System. Configuration. SingleTagSectionHandler, System. Configuration. DictionarySectionHandler, and System. Configuration. NameValueSectionHandler.

Different types not only have different configuration section settings, but also have different operations to access the configuration file. The following is an example of a configuration file that contains the three different types.

Program code:

 

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="Test1" type="System.Configuration.SingleTagSectionHandler"/> <section name="Test2" type="System.Configuration.DictionarySectionHandler"/> <section name="Test3" type="System.Configuration.NameValueSectionHandler" /> </configSections> <Test1 setting1="Hello" setting2="World"/> <Test2> <add key="Hello" value="World" /> </Test2> <Test3> <add key="Hello" value="World" /> </Test3> </configuration> 

We will describe the above custom configuration section. In the declaration section, use <section name = "Test1" type = "System. Configuration. SingleTagSectionHandler"/> to declare a Configuration section named Test1 and its type is
SingleTagSectionHandler. In the Configuration Setting section, use <Test1 setting1 = "Hello" setting2 = "World"/> to set a configuration section. Its first value is Hello, the second value is World. Of course there can be more. Other
The two configuration sections are similar to this one.
The following describes how to access these custom configuration sections in the program. We used the static method GetConfig of the ConfigurationSettings class to get information about the custom configuration section.

Program code:
Public static object GetConfig (string sectionName );

The following code accesses these three configuration sections:

Program code:

// Access configuration section Test1 IDictionary IDTest1 = (IDictionary) ConfigurationSettings. getConfig ("Test1"); string str = (string) IDTest1 ["setting1"] + "+ (string) IDTest1 [" setting2 "]; MessageBox. show (str); // output Hello World // method 2 string [] values1 = new string [IDTest1.Count] In access configuration section Test1; IDTest1.Values. copyTo (values1, 0); MessageBox. show (values1 [0] + "" + values1 [1]); // output Hello World // access configuration section Test2 IDictionary IDTest2 = (IDictio Nary) ConfigurationSettings. getConfig ("Test2"); string [] keys = new string [IDTest2.Keys. count]; string [] values = new string [IDTest2.Keys. count]; IDTest2.Keys. copyTo (keys, 0); IDTest2.Values. copyTo (values, 0); MessageBox. show (keys [0] + "" + values [0]); // access configuration section Test3 NameValueCollection nc = (NameValueCollection) ConfigurationSettings. getConfig ("Test3"); MessageBox. show (nc. allKeys [0]. toString () + "" + nc ["Hello"]); // Output Hello World. We can see from the code above that different types are returned by GetConfig in different ways. [Table] configuration section handler | return type [br] [/table] SingleTagSectionHandler Systems. Collections. IDictionary DictionarySectionHandler Systems. Collections. IDictionary NameValueSectionHandler Systems. Collections. Specialized. NameValueCollection

3.2 custom configuration section group
The configuration section group uses the <sectionGroup> element to group similar configuration sections to the same group. In the configuration section group declaration section, the configuration section inclusion elements are created, the configuration section group is declared in the <configSections> element, and the sections belonging to the group are placed in
<SectionGroup> element. The following is an example of a configuration file containing the configuration section group:

Copy the Code as follows:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="TestGroup"> <section name="Test" type="System.Configuration.NameValueSectionHandler"/> </sectionGroup> </configSections> <TestGroup> <Test> <add key="Hello" value="World"/> </Test> </TestGroup> </configuration>

 

Related Article

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.