转贴自:http://www.cnblogs.com/jackyrong/archive/2005/05/06/150067.html
vs.net 2005中的ConfigurationManager,代替了原来的configurationsettings。用法大概如下,详细的看MSDN
读取配置:
ConfigurationManager.AppSettings["MyKey"]
读取数据库的配置(这个估计大家最经常用了)
在web.config中
<connectionStrings>
<add name="AppConnectionString1" connectionString="server=localhost;database=northwind;uid=sa;password=xxxx;"/>
</connectionStrings>
在程序中如下读取:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString);
GETSECTION方法:
ConfigurationManager.GetSection("mySection")
其他的方法大概描述摘录如下:
GetWebAppSection
With the GetWebAppSection you can get the specified section from the web applications configuration file.
Note: This method retrieves the specified configuration section from the configuration file located at the root folder of your Web application. If you want to retrieve the configuration section from the current Web application directory use theGetSectionmethod.
RefreshSection
RefreshSection will refresh the specified section so next time its requested, it will be re-read from disk.
With the following method, you will have the ability to programmatically change the settings within the configuration files. Those methods will return a Configuration class (The Configuration class has properties to get sections from the configuration file you want to edit, and by using the Save or SaveAs method, you can save the settings):
OpenMachineConfiguration
With the OpenMachineConfiguration, you configure the machine.config file.
OpenMappedMachineConfiguration
With the OpenMappedMachineConfiguration, you can open a specified configuration file.
OpenExeConfiguration
With the OpenExeConfiguration, you can open the client’s configuration file.
OpenMappedExeConfiguration
With the OpenMappedExeConfiguration, you can open the specified client configuration.
OpenWebConfiguration
With the OpenWebConfiguration, you can open the web applications configuration file.
OpenMappedWebConfiguration
With the OpenMappedWebConfiguration, you can open a specified web configuration file.