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

标签 c# winforms app-config

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

相关文章:

c# - 应用程序是否需要 app.config

.net - 防止 .NET 配置文件在安装过程中被覆盖

.net - 将连接字符串传递给 dll?

c# - 在 WPF 中向 DataGrid 添加行?

winforms - 使用 WinForms 设计器时,是否始终需要将 DPI 设置为 96?

c# - 如何使窗口可拖动(C# Winforms)?

c# - 如何在 PictureBox 上捕获鼠标坐标?

c# - Windows Phone 的 Windows 应用商店应用程序上的照片捕获

c# - 为什么这不会产生溢出异常?

c# - 在 LINQ 中使用 String.Split() 和 Contains() ?