应用程序未读取 user.config

application not reading from user.config

我有 Visual studio 解决方案,其中包含 asp.net 和控制台应用程序项目。在控制台应用程序中,我有 app.config 我这样声明的,它应该从 user.config

读取
  <appSettings  file="user.config">

  </appSettings>

这是我的user.config

<appSettings>

  <add key="MyKey" value="Test" />

</appSettings>

然后在我的代码中我尝试像

那样获取
 string myKey = ConfigurationManager.AppSettings["MyKey"];

但它给了我空值。这里有什么问题以及如何解决?

更新 1 我已将 app.config 和 user.config

的“复制到输出目录”设置为“始终复制”

user.config 必须存在于与 app.config 相同的目录中;如果在运行时找不到该文件,它将使用 app.config 中的值(不存在),因此您会得到 null。确保 user.config 配置为在生成时复制到输出目录,并且生成操作设置为 "None".

构建项目后,输出目录中是否存在两个 config 文件?