gpt4 book ai didi

c# - 从外部配置文件中读取部分 appSettings

转载 作者:太空狗 更新时间:2023-10-29 21:38:41 29 4
gpt4 key购买 nike

我想从名为 secrets.config 的外部配置文件中读取控制台应用程序的 appSettings 的一部分,而其余部分我想从 app.config 中读取。

目前,我已经设置好了,但它似乎没有从 secrets.config 中读取,甚至没有告诉我读取失败。

在我的 app.config 中

<appSettings file = "secrets.config">
<add key = "Foo" value = "Bar" />
</appSettings>

在 secrets.config 中,它与 app.config 在同一个文件夹中

<appSettings>
<add key = "Secret" value = "Tiger" />
</appSettings>

在我的代码中

var secret = ConfigurationManager.AppSettings["Secret"];

// secret turns out to be null

最佳答案

原来是我把外部文件的路径写错了

来自文档 on this page :

指定的路径是相对于主配置文件的。对于 Windows 窗体应用程序,这将是二进制文件夹(例如/bin/debug),而不是应用程序配置文件的位置。对于 Web 窗体应用程序,该路径相对于应用程序根目录,即 web.config 文件所在的位置。

我将路径更改为以下路径:

<appSettings file = "..\..\secrets.config">
</appSettings>

关于c# - 从外部配置文件中读取部分 appSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33101748/

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