How to read the configuration file in C #

Turn: http://www.cnblogs.com/Gerry-BJ/archive/2008/08/06/1261712.html

How to read the configuration file in C #

November 6, 2007 Tuesday 12:53 PM

1. Profiles Overview:
application configuration file is a standard XML file, XML tags and attributes are case-sensitive. It can be changed as needed, developers can use the configuration file to change settings without having to recompile the application. Root configuration file is configuration. We often visit is appSettings, it is a .Net predefined configuration section. Architecture profile is like we often use the following form. About a first impression, there will be a more clear understanding by the examples that follow. The following "configuration section" may be understood as a configuration XML nodes.

Common mode configuration file:

<Configuration>
        <configSections> // declaration area configuration section, and the section containing the configuration namespace declaration
                <section> // declare configuration section
            <sectionGroup> // set configuration section defines
                the configuration section declaration <section> // configuration section group
        <appSettings> // predefined configuration section
        <Custom element for configuration section> // configuration section settings area

2. Only appSettings section of the configuration file and access methods

Here is an example of the most common application configuration file, 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 such a configuration file method.

string _connectionString=ConfigurationSettings.AppSettings["connectionstring"];

AppSettings ConfigurationSettings use the static properties of the class can be directly method of configuring the configuration file. This type of property is NameValueCollection.

3. Custom Profile
3.1 custom configuration section

A user-defined configuration section, the configuration file is divided into two parts: one </ configSections> configuration section declaration configuration section (upper profile mode "<section>") in <configSections>, in addition after setting <configSections> </ configSections> configuration section (upper profile mode "<Custom element for configuration section>"), somewhat similar to a variable declaration, the use of the same. Statement declares a configuration file is as follows:

<Section name = "" of the type = "" />
<Section>: declare a new configuration section, you can create a new configuration section.

name: the name of the custom configuration section.

type: the type of custom configuration sections, including System.Configuration.SingleTagSectionHandler, System.Configuration.DictionarySectionHandler, System.Configuration.NameValueSectionHandler.

Different type not only how the configuration section is not the same, there are differences in the operation of the configuration file was last accessed. Here we give an example of a configuration file, it includes the three different type.

<?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 custom configuration section above will be explained. In the declarations section using the <section name = "Test1" type = "System.Configuration.SingleTagSectionHandler" /> configuration section that declares a name Test1, type SingleTagSectionHandler. Configuration section portion is provided using <Test1 setting1 = "Hello" setting2 = "World" /> provided a configuration section, a first set of values that is the Hello, the second value is the World, of course also more . The other two configuration sections and the similar.
Here we look at how to access these custom configuration section in the program. We used the static method GetConfig ConfigurationSettings class to obtain information about custom configuration section.

public static object GetConfig(string sectionName);

Here is the code to access the three configuration sections:

//访问配置节Test1
            IDictionary IDTest1 = (IDictionary)ConfigurationSettings.GetConfig("Test1");
            string str = (string)IDTest1["setting1"] +" "+(string)IDTest1["setting2"];
            MessageBox.Show(str);        //输出Hello World

            //访问配置节Test1的方法2
            string[] values1=new string[IDTest1.Count];
            IDTest1.Values.CopyTo(values1,0);
            MessageBox.Show(values1[0]+" "+values1[1]);    //输出Hello World
            //访问配置节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]);
            //访问配置节Test3
            NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("Test3");
MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]);    //输出Hello World

By the above code we can see different types of different type by GetConfig returned to obtain the specific configuration of the content are not the same. Configuration section handler
return type
SingleTagSectionHandler
Systems.Collections.IDictionary
DictionarySectionHandler
Systems.Collections.IDictionary
NameValueSectionHandler
Systems.Collections.Specialized.NameValueCollection

3.2 custom configuration section group
configuration section group using <sectionGroup> element, a configuration similar to the section assigned to the same group. Group claim configuration section creates the configuration portion comprises a section element, configuration section declarations groups <configSections> element, belonging to segments were placed and <sectionGroup> element of the group. The following is an example of the configuration of the profile section of the group comprising:

<?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>
下面是访问这个配置节组的代码:
            NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("TestGroup/Test");
            MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]);    //输出Hello World

Reproduced in: https: //my.oschina.net/cookblack/blog/621362

Guess you like

Origin blog.csdn.net/weixin_34288121/article/details/92046052