[开发笔记]WebConfigurationManager和ConfigurationManager

今天在写程序时偶然发现有的示例代码中是用WebConfigurationManager获取web.config中的配置信息的,因为之前一直都是用的ConfigurationManager,所以简单的深究了一下,并做个小小的总结。

MSDN的说明:

使用 WebConfigurationManager 可以访问计算机和应用程序信息。

使用 WebConfigurationManager 是处理与 Web 应用程序相关的配置文件的首选方法。对于客户端应用程序,请使用 ConfigurationManager

 

不知道这里是否可以理解为:

1. 对于web应用程序而言,建议优先使用WebConfigurationManager ;但该方法不适用于客户端应用程序比如winform,WPF程序。

2. ConfigurationManager ,即适用于web应用程序,也适用于客户端应用程序,但对于客户端应用程序来说更好。

 

其他整理:

        //对于客户端应用程序,请使用 ConfigurationManager。
        protected static string connectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();

        protected static string appconfigString = ConfigurationManager.AppSettings[""].ToString();

        //使用 WebConfigurationManager 是处理与 Web 应用程序相关的配置文件的首选方法。
        protected static string webconnectionString = WebConfigurationManager.ConnectionStrings[""].ToString();

        protected static string webappconfigString = WebConfigurationManager.AppSettings[""].ToString();


        //Web.config中的配置节点:

        //ConnectionStrings
        //<connectionStrings>
        //  <add name="ConnStr" connectionString="Data Source=.;user ID=sa;Pwd=123456;Database=database;"/>
        //</connectionStrings>

        //AppSettings
        //<appSettings>
        //  <add key="" value=""/>
        //</appSettings>


        //写在 <appSettings >中用System.Configuration.ConfigurationManager.AppSettings["name"]检索值。 
        //写在 <ConnectionStrings>中用System.Configuration.ConfigurationManager.ConnectionStrings["name"]检索值。


        //注意:
        //System.Web.Configuration  默认没有在程序中添加引用,所以直接using无法using出来。


附:

转载请注明出处。

版权
作者:酷小孩

出处:http://www.cnblogs.com/babycool/

本文首发博客园,版权归作者跟博客园共有。

转载必须保留本段声明,并在页面显著位置给出本文链接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/babycool/p/3112513.html