appname.exe.config 未在 windows 应用程序上创建

appname.exe.config not created on windows application

当我构建 windows 应用程序时。它不起作用,因为它无法读取我的 app.config 文件。我查看了 bin 目录,它没有 appname.exe.config 文件。如果我手动复制 app.config 并将其重命名为 appname.exe.config 应用程序将工作..但是当我构建项目时,这个文件永远不会自动创建。我错过了什么?为什么我的项目没有创建它?我查看了整个文件夹结构,没有其他配置文件。

假设您正在使用 Visual Studio 在解决方案资源管理器中单击您的配置文件,然后在“属性”面板中将“复制到输出目录”设置为“不复制”以外的其他内容。

查看 App.config 属性,应该是:
构建操作:None
CopyToOutputDirectory:不要复制
自定义工具和自定义工具命名空间应为空

也尝试重建项目。右键单击项目 -> 重建

我认为这里的每个人都在给你虚假信息。我认为@bowlturner 是对的。根据 Microsoft 的说法,您确实需要将 app.configCopy to output directory 属性 设置为 Copy AlwaysCopy if newer。来自 Microsoft's MSDN Site:

When you develop in Visual Studio, place the source configuration file for your app in the project directory and set its Copy To Output Directory property to Copy always or Copy if newer. The name of the configuration file is the name of the app with a .config extension. For example, an app called myApp.exe should have a source configuration file called myApp.exe.config.

Visual Studio automatically copies the source configuration file to the directory where the compiled assembly is placed to create the output configuration file, which is deployed with the app.

正确的设置是:

  • Build Action = None
  • Copy to Output Directory = Do not copy

非常重要,项目文件需要包含这个元素:

  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>

唯一对我有用的是完全删除 bin\ 和 obj\。重建,文件创建正常。