【发布时间】:2026-01-29 07:15:01
【问题描述】:
我有包含 asp.net 和控制台应用程序项目的 Visual Studio 解决方案。在控制台应用程序中,我有 app.config 我这样声明它应该从 user.config 读取
<appSettings file="user.config">
</appSettings>
这是我的 user.config
<appSettings>
<add key="MyKey" value="Test" />
</appSettings>
然后在我的代码中我尝试获取类似
string myKey = ConfigurationManager.AppSettings["MyKey"];
但它给了我null。这里有什么问题以及如何解决?
更新 1 我已将 app.config 和 user.config 的 Copy to Output 目录设置为 Copy Always
【问题讨论】:
-
您确定 user.config 存在于应用程序目录中吗?你每次都把它复制到输出目录吗?为什么不尝试指定该文件的绝对路径。
-
您是否在 appconfig 中有一个
部分并进行了 System.Configuration 参考? -
@Bebolicious offcourse 我在 app.config 中有配置部分。我只是没有包含在 SO
-
@James 你不能责怪他们问什么时候它不包含在问题中。
-
@Amy 没有怪罪。我只是告诉我没有包含在 SO