在System.Configuration命名空间下死活找不到ConfigurationManager类

这里谈到是三层编程在CLASS里调用的问题:

开始使用VS 2005,习惯性的使用ConfigurationSettings类来读取应用程序配置文件的信息时,却被编译器提示说: 警告 1 “System.Configuration.ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”

于是转而想找到那个ConfigurationManager类来使用,结果在System.Configuration命名空间下死活找不到ConfigurationManager类, 无奈,求助于网络,才知道原来还要添加对System.Configuration.dll 文件的引用~~

事后才留意到,原来MS对这一点还是提示了一下的,只不过比较隐晦而已—— This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings

注意到没有,上面这段话多了System.Configuration!这段~ 如:    string we=System.Configuration.ConfigurationManager.AppSettings["sql"];       

如:   System.Web.HttpContext.Current.Cache["Cache1"] = "Value1"; System.Web.dll

可以使用ConfigurationManager来一次性读取节点的所有配置项,具体步骤如下: 1. 引用System.Configuration和System.IO命名空间。 2. 使用ConfigurationManager.OpenExeConfiguration方法打开应用程序.config文件。 3. 使用Configuration.Sections属性读取所有配置节点。 4. 使用ConfigurationSection.Element属性按节点名称读取配置节点。 5. 使用ConfigurationSection.SectionInformation.GetRawXml方法获取节点的XML字符串。 6. 使用XmlDocument或XDocument等XML解析器解析XML字符串,获取配置项的键值对。 示例代码如下: ```csharp using System.Configuration; using System.IO; // 打开应用程序.config文件 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // 读取所有配置节点 ConfigurationSectionCollection sections = config.Sections; // 循环读取节点的所有配置项 foreach (ConfigurationSection section in sections) { // 按节点名称读取配置节点 ConfigurationSection configSection = config.GetSection(section.SectionInformation.Name); // 获取节点的XML字符串 string xml = configSection.SectionInformation.GetRawXml(); // 解析XML字符串,获取配置项的键值对 XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlNodeList nodes = doc.FirstChild.ChildNodes; foreach (XmlNode node in nodes) { string key = node.Attributes["key"].Value; string value = node.Attributes["value"].Value; // 处理配置项的键值对 } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值