您好,我有以下 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
值
异常无法将 Keyvalueinternalcollection 转换为哈希表
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/