Writing this blog is to practice my poor English!
1 ConfigurationManager Class
When we’d like to use ConfigurationManager,we should reference the configuration DLL. The detail step is the following content:
Project->Add Reference…->Browse, then you can choose the target DLL in folders such as
C:\Program Files\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v4.0
OR
C:\Program Files\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v4.5
Either will be suitable.
1.1 Read Data From Configure File
String value = ConfigurationManager.AppSettings["Key"];
1.2 Write Data to Configure File
1) Write Data to source file
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("new_Key","Value");
config.AppSettings.Settings["source_Key"].Value= "value";
config.Save();
2) Write Data to another file
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string configFile = “…\\app.config”;
config.SaveAs(configFile,ConfigurationSaveMode.Full);
config.AppSettings.Settings.Add("Key","Value");
config.AppSettings.Settings["Key"].Value= "Value";
config.Save();
1.3 Access only appSetting configuration file
The following file is a common application configure example with appSettings:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="name "value="jackey" />
<add key="path "value="c://app.config" />
</appSettings>
</configuration>
1) Read method:
String name = ConfigurationManager.AppSettings["name"];
AppSettings property is read-only, we can’t have update or insert operations on those appSetting Sections.
2) Write method:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["name"].Value= "value";
config.Save();
1.4 Custom Configuration File
The configSections element specifies configuration section and handler declarations. The configuration section isincluded in <configSections></configSections>, then use the definedsection type to declare the section. If the configSections element is in a configuration file, the configSectionselement must be the first child element of the configuration element.
The following content presents how to use the configSections:
<?xml version="1.0"encoding="utf-8" ?>
<configuration>
<configSections>
<section name="type1"type="System.Configuration.SingleTagSectionHandler"/>
<section name="type2"type="System.Configuration.DictionarySectionHandler"/>
<section name="type3"type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<startup>
<supportedRuntimeversion="v4.0"sku=".NETFramework,Version=v4.0" /></startup>
<appSettings>
<add key="name"value="111" />
<add key="name1"value="q" />
</appSettings>
<type1 setting1="hello1"setting2="world1" />
<type2>
<add key="hello2"value="world2"/>
</type2>
<type3>
<add key="hello3"value="world3"/>
</type3>
</configuration>
The example shows that the configSections type mainly includes:
- System.Configuration.SingleTagSectionHandler
- System.Configuration.DictionarySectionHandler
- System.Configuration.NameValueSectionHandler
Now we will discuss how to use this custom sections:
1) Read type1 Section
IDictionary IDTest1 = (IDictionary)ConfigurationManager.GetSection("type1");
String str = (String)IDTest1["setting1"]+ " " + (String)IDTest1["setting2"];
Console.WriteLine(str);
We can use another way:
IDictionary IDTest1_1 = (IDictionary)ConfigurationManager.GetSection("type1");
string[] values1 = new string[IDTest1_1.Count];
IDTest1_1.Values.CopyTo(values1,0);
Console.WriteLine(values1[0]+ " " + values1[1]);
2) Read type2 Section
IDictionary IDTest2 = (IDictionary)ConfigurationManager.GetSection("type2");
string[] keys = new string[IDTest2.Keys.Count];
string[] values = new string[IDTest2.Keys.Count];
IDTest2.Keys.CopyTo(keys, 0);
IDTest2.Values.CopyTo(values, 0);
Console.WriteLine(keys[0] + "" + values[0]);
3) Read type3 Section
NameValueCollection nc = (NameValueCollection)ConfigurationManager.GetSection("type3");
Console.WriteLine(nc.AllKeys[0].ToString()+ " " + nc["hello3"]);
1.5 Custom Section Group
We can use <sectionGroup> element to classify the similar Sections to a same group. The sectionGroup Declaration creates the sectiontypes which is used to declare a section. For examples:
<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
<sectionGroup name="testGroup1">
<section name="type4"type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<testGroup1>
<type4>
<add key="hello4"value="world4"/>
</type4>
</testGroup1>
</configuration>
The read method:
NameValueCollection nc4 = (NameValueCollection)ConfigurationManager.GetSection("testGroup1/type4");
Console.WriteLine(nc4.AllKeys[0].ToString() + "" + nc4["hello4"]);
Over!
3174



被折叠的 条评论
为什么被折叠?



