Tutorials to .com

Tutorials to .com » Dotnet » Foundation » . Net2.0 read and write configuration files using ConfigurationManager

. Net2.0 read and write configuration files using ConfigurationManager

Print View , by: iSee ,Total views: 6 ,Word Count: 383 ,Date: Sat, 22 Aug 2009 Time: 3:59 PM

. net1.1 in if you need to read and write operations and flexible configuration file is not very convenient, the usual package in the project management of a configuration file to read and write operations. In. Net2.0 used ConfigurationManager and WebConfigurationManager category management can be a very good profile, ConfigurationManager in System.Configuration class, WebConfigurationManager of the System.Web.Configuration. According to MSDN's explanation for the Web application configuration, it is recommended to use System.Web.Configuration.WebConfigurationManager category, and do not use System.Configuration.ConfigurationManager category.

Let me give a simple example shows how to use the configuration file WebConfigurationManager operation:
/ / Open the configuration file
Configuration config = WebConfigurationManager.OpenWebConfiguration ("~");
/ / access appSettings node
AppSettingsSection appSection = (AppSettingsSection) config.GetSection ( "appSettings");
/ / Add nodes in the appSettings element
appSection.Settings.Add ( "addkey1", "key1's value");
appSection.Settings.Add ( "addkey2", "key2's value");
config.Save ();

After running the code can see changes in the configuration file:

<appSettings>
<add key="addkey1" value="key1's value" />
<add key="addkey2" value="key2's value" />
</ appSettings>
Modify, and delete nodes or attributes is also very convenient:

/ / Open the configuration file
Configuration config = WebConfigurationManager.OpenWebConfiguration ("~");
/ / Access appSettings node
AppSettingsSection appSection = (AppSettingsSection) config.GetSection ( "appSettings");
/ / Remove element nodes appSettings
appSection.Settings.Remove ( "addkey1");
/ / Modify the appSettings element node
appSection.Settings [ "addkey2"]. Value = "Modify key2's value";
config.Save ();
Configuration file:
<appSettings>
<add key="addkey2" value="Modify key2's value" />
</ appSettings>
Reference: http://msdn2.microsoft.com/en-us/library/ms228060.aspx

http://justicfu.cnblogs.com/archive/2006/06/21/431632.html


.NET foundation Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.