gpt4 book ai didi

c# - 从 app.config c# 中读取所有键值

转载 作者:太空宇宙 更新时间:2023-11-03 23:36:05 30 4
gpt4 key购买 nike

您好,我有以下 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com