gpt4 book ai didi

c# - 从 app.config 文件读取

转载 作者:行者123 更新时间:2023-11-30 14:26:53 25 4
gpt4 key购买 nike

给定一个 app.config 文件,如果我感兴趣的那些键都遵循我首先建立的模式,我如何读取键和值。例如,我知道我的所有键的名称都将遵循

的模式
<add key="my_*_Def"  value = "someValue">

问题是我想编写一个程序,我可以将这些配置文件提供给它,然后我的程序会在该文件中找到该模式并将它们提供给我进行进一步处理。

除了将其视为文本文件并逐行读取之外,还有更好的编写方法吗?

最佳答案

你可以使用

ConfigurationManager.AppSettings.AllKeys

在 app.config 文件中查找所有键。 ( link )

要在另一个文件中尝试,您需要使用 ConfigurationManager.OpenExeConfiguration Method (String) 获取配置

所以;

var config = ConfigrationManager.OpenExeConfiguration("foo.exe.config");
var keys = config.AppSettings.AllKeys;

关于c# - 从 app.config 文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34363091/

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