Custom application configuration file (app.config)

Source: Internet
Author: User
Tags allkeys config contains count key tostring
Program  

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 recompiling the application. The root node of the configuration file is configuration. What we often visit is appsettings, which is made up of. NET predefined configuration section. The configuration file we use often has a schema that looks like the following form. There is probably an impression that there is a clearer understanding of the following examples. The following "configuration section" can be understood as a node for configuring an XML.

Common configuration file Mode:


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

2. Only appsettings section of the configuration file and Access methods

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


<?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>

Let's look at how this configuration file is done.

String _connectionstring=configurationsettings.appsettings["connectionString"];

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

3. Custom configuration file
3.1 Custom Configuration section

A user-defined configuration section that is divided into two parts in a configuration file: The first is to declare the configuration section in the <configsections></configsections> configuration section ("<section>" in the configuration file mode above) , in addition to setting the configuration section after the <configsections></configsections > (in the configuration file mode above, "<custom element for configuration sections > "), somewhat similar to a variable declared before use. The statement declaring a configuration file is as follows:

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

Name: Names of 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 configuration sections differently, but also have differences in the operation of the last access profile. Here's an example of a configuration file that contains these three different types.

<?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 Declarations section use <section name= "Test1" type= "System.Configuration.SingleTagSectionHandler"/> declares a configuration section its name is Test1, Type is SingleTagSectionHandler. In the Set configuration section section, use <test1 setting1= "Hello" setting2= "World"/> Set up a configuration section, its first set value is Hello, the second value is world, of course, there can be more. The other two configuration sections are similar to this one.
Here's how you can access these custom configuration sections in your program. We have used the static method GetConfig of the ConfigurationSettings class to get the information for the custom configuration section.

public static Object GetConfig (string sectionname);

Here is the code to access these three configuration sections:

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 to access configuration section Test1 2
String[] Values1=new String[idtest1.count];
IDTest1.Values.CopyTo (values1,0);
MessageBox.Show (values1[0]+ "" +values1[1]); Output Hello World

Access configuration section Test2
IDictionary 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 Test3
NameValueCollection nc= (NameValueCollection) configurationsettings.getconfig ("Test3");
MessageBox.Show (NC. Allkeys[0].    ToString () + "" +nc["Hello"]); Output Hello World

We can see from the above code that different types are different from the type returned by GetConfig, and the exact way to get the configuration content is not the same. Configuration section Handlers
return type

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 similar configuration sections 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 belonging to the group in the <sectionGroup> element. The following is an example of a configuration file that contains a configuration section group:

<?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>
Here is the code to access this configuration section group:
NameValueCollection nc= (NameValueCollection) configurationsettings.getconfig ("Testgroup/test");
MessageBox.Show (NC. Allkeys[0].    ToString () + "" +nc["Hello"]); Output Hello World



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.