嗨,我有以下app.config文件

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
<appSettings>
<add key="DayTime" value="08-20" />
<add key="NightTime" value="20-08" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
<add key="GridMode" value="1"/>
</appSettings>


我需要一次读取所有键并将其存储在Dictionary之类的位置。

我尝试了以下代码,但给了我null

除了Cannot convert Keyvalueinternalcollection to hashtable

var section = ConfigurationManager.GetSection("appSettings") as Hashtable;


如何读取所有键及其值?

最佳答案

哈希表版本。

Hashtable table = new Hashtable((from key in System.Configuration.ConfigurationManager.AppSettings.Keys.Cast<string>()
                                 let value= System.Configuration.ConfigurationManager.AppSettings[key]
                                 select new { key, value }).ToDictionary(x => x.key, x => x.value));


评论其他答案

System.Configuration.ConfigurationManager.AppSettings是预定义的“ appSettings”部分,请使用它。

关于c# - 从app.config C#中读取所有键值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30572923/

10-13 06:57