【转载】WebConfigurationManager和ConfigurationManager
2024-09-30 04:21:28
今天在写程序时偶然发现有的示例代码中是用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出来。
复制代码
最新文章
- Oracle学习笔记十三 触发器
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
- [Cocos2d-x For WP8]DrawPrimitives画图
- 【mysql的紧急应用】
- CSS中 opacity的设置影响了index(层数)的改变
- 前端MVC框架对比
- HDU 4612 Warm up(手动扩栈,求树上哪两个点的距离最远)
- HDU 4957 Poor Mitsui
- Spring Session - Spring Boot
- java map集合的知识
- 使用Xilinx UART-LITE IP实现串口--逻辑代码实现
- ruby-操作mysql
- 浏览器内核、渲染引擎、JS引擎简介
- python基础技巧综合训练题1
- 用Hadoop构建电影推荐系统
- 怎么把<;li>;标签里的内容付给文本框
- Linux-数据库1
- 用newInstance与用new是区别的
- Java发送短信
- 斯坦福大学Andrew Ng - 机器学习笔记(8) -- 推荐系统 &; 大规模机器学习 &; 图片文字识别