Configuration file (app. Config file)

Source: Internet
Author: User

1. configuration file Overview:
The application configuration file is a standard XML file, and XML tags and attributes are case-sensitive. It can be changed as needed, and developers can use the configuration file to change the settings without having to recompile the application. The root node of the configuration file is
Configuration We often visit appsettings, which is made up of. NET pre-defined configuration section. The schema of the configuration file that we often use is like the following form. First of all, there is an impression that the following example will have a clearer understanding. The following "
Configuration section "can be understood as a node that configures an XML.

Common Configuration file modes:

<configuration>
<configSections>//configuration section Declaration area, including configuration sections and namespace declarations
<section>//configuration section Declaration
<sectionGroup>//Define configuration section groups
Configuration section declarations in <section>//configuration section groups
<appSettings>//Pre-defined configuration section
<custom element for configuration section>//configuration section Settings area

2. Only appsettings section configuration file and access method

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

Program code:

 <?xml version= 1.0   " Encoding="  utf-8   "?> <configuration> <appSettings> <add key="  connectionstring   value="  Span style= "color: #800000;" >user Id=sa;data source=.; Password=;initial catalog=test; Provider=SQLOLEDB.1;   "/> <add key="  templatepath   value="  template  /> </appSettings> </configuration> 


The following is a look at how this configuration file is used.

Program code:
String _connectionstring=configurationsettings.appsettings["connectionString"];

You can use the static properties of the ConfigurationSettings class to appsettings the configuration information in a direct method configuration file. The type of this property is NameValueCollection.


3. Custom configuration Files

3.1 Custom Configuration section

A user-defined configuration section that is divided into two parts in the configuration file: First, the configuration section is declared in the <configsections></configsections> configuration section ("<section>" in the configuration file mode above) , in addition to the <configSections></
configsections > After setting up the configuration section (above in profile mode "< Custom element for configuration section>"), a bit like a variable is declared first and then used the same. The statement that declares a configuration file is as follows:

<section name= "" type= ""/>
<section>: Declares a new configuration section to create a new configuration section.

Name: The names of the custom configuration sections.

Type: The types of custom configuration sections, mainly including System.Configuration.SingleTagSectionHandler, System.Configuration.DictionarySectionHandler, System.Configuration.NameValueSectionHandler.

Different types not only set the configuration section differently, but there are also differences in the operation of the last access configuration file. Let's give an example of a configuration file that contains these 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 describe the custom configuration section above. In the Declaration section use <section name= "Test1" type= "System.Configuration.SingleTagSectionHandler"/> declares a configuration section its name is Test1, the type is
SingleTagSectionHandler. In the Set configuration section section, use <test1 setting1= "Hello" setting2= "World"/> Set a configuration section, its first setting value is Hello, the second value is world, and of course there are more. Other of
The two configuration sections are similar to this one.
Let's look at how these custom configuration sections are accessed in the program. We have used the static method of the ConfigurationSettings class GetConfig to get the information for the custom configuration section.

Program code:
public static Object GetConfig (string sectionname);

Here is the code to access the three configuration sections:

Program code:

//Access configuration section Test1IDictionary IDTest1 = (IDictionary) configurationsettings.getconfig ("Test1"); stringstr = (string) idtest1["setting1"] +" "+(string) idtest1["setting2"]; MessageBox.Show (str); //Output Hello world//to access configuration section Test1 Method 2string[] values1=New string[Idtest1.count]; IDTest1.Values.CopyTo (Values1,0); MessageBox.Show (values1[0]+" "+values1[1]);//Output Hello world//Access configuration section Test2IDictionary IDTest2 = (IDictionary) 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 Test3NameValueCollection nc= (NameValueCollection) ConfigurationSettings.GetConfig ("Test3"); MessageBox.Show (NC. allkeys[0]. ToString () +" "+nc["Hello"]);//Output Hello worldAs we can see from the above code, different types are returned by GetConfig differently, and the exact way to get the configuration content is different. [Table] configuration section handlers|return type [BR] [/Table] SingleTagSectionHandler Systems.Collections.IDictionary DictionarySectionHandler Systems.Collections.IDictionary NameValueSectionHandler Systems.Collections.Specialized.NameValueCollection

3.2 Custom Configuration section groups
A configuration section group uses the <sectionGroup> element to divide a similar configuration section into the same group. The configuration section Group Declaration section creates the containing element of the configuration section, declares the configuration section group in the < configsections> element, and places the section that belongs to the group in the
The <sectionGroup> element. The following is an example of a configuration file that contains a configuration section group:

Copy the code 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>

Configuration file (app. Config file)

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.