子项目的 App.config 未复制到输出

发布于 2024-12-27 15:31:20 字数 381 浏览 0 评论 0原文

我有一个包含两个可执行项目的解决方案。

Main.exe 依赖于Sublined.exe

这些项目都有 App.config 文件,因此在它们各自的输出目录中,我有 Main.exe.configSubordination.exe.config

当我构建 Main.exe 时,Subordination.exe 被复制到 Main 的输出目录中,但 Sublined.exe.config 却没有。

是否有标准方法告诉 Visual Studio 执行此操作?

I have a solution with two executable projects.

Main.exe is dependent on Subordinate.exe.

These projects both have App.config files, so in their respective output directories, I have Main.exe.config and Subordinate.exe.config.

When I build Main.exe, Subordinate.exe is copied into Main's output directory, but Subordinate.exe.config is not.

Is there a standard way to tell Visual Studio to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灵芸 2025-01-03 15:31:20

我发现的另一种方法是将 app.config 文件作为链接文件添加到依赖项目中,例如,在这里,您将在 Main.exe 的项目中添加到 Subordination.exe 的 app.config 的链接,并将其设置为复制到输出构建时的目录。然后,您可以通过编辑项目文件中的 元素来更改在构建时复制到 Main.exe 输出目录的文件的名称,例如

<None Include="..\Subordinate.ProjectFolder\app.config">
  <Link>Subordinate.exe.config</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

:您必须对生成的配置文件的名称进行硬编码,但这并不比 AfterBuild 方法更糟糕,而且我发现这种方法更加透明,并且它消除了您提到的构建顺序依赖性问题。

Another way I found is by adding the app.config file as a linked file to the dependent project, e.g. here you would add a link to Subordinate.exe's app.config into Main.exe's project, and set it to be copied to the output directory when built. You can then change the name of the file that is copied to Main.exe's output directory at build time by editing the <Link> element in your project file, something like:

<None Include="..\Subordinate.ProjectFolder\app.config">
  <Link>Subordinate.exe.config</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

This has the same issue that you have to hardcode the name of the generated config file, but that's no worse than the AfterBuild approach, and I find this method a little more transparent, and it removes the build-order dependency issue that you mentioned.

不疑不惑不回忆 2025-01-03 15:31:20

右键单击Main 项目并选择编辑项目文件。添加 AfterBuild 事件:

  <Target Name="AfterBuild">
    <Copy SourceFiles="..\Subordinate\bin\$(Configuration)\Subordinate.exe.config" 
          DestinationFolder="$(TargetDir)" />
  </Target>

Right click on the Main project and select Edit Project File. Add an AfterBuild event:

  <Target Name="AfterBuild">
    <Copy SourceFiles="..\Subordinate\bin\$(Configuration)\Subordinate.exe.config" 
          DestinationFolder="$(TargetDir)" />
  </Target>
水水月牙 2025-01-03 15:31:20

看看这个答案(基于@theyetiman的工作)。它主要需要修改 app.config 文件所属的项目。 使用项目保持不变。

<ItemGroup>
  <None Include="app.config" />
</ItemGroup>
<ItemGroup>
  <None Include="app.config">
    <Link>$(TargetFileName).config</Link>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>

另请注意,对于 Visual Studio 2017,不再需要进行此修改。如果您打开包含上述解决方法的项目的解决方案,错误窗口中会显示类似于以下内容的警告:

无法将文件“app.config”添加到项目中。
无法添加指向文件 ...\app.config 的链接。
该文件位于项目目录树中。

添加一个比较 $(VisualStudioVersion) 的条件以避免此警告并保持向后兼容性:

<ItemGroup>
  <None Include="app.config" />
</ItemGroup>
<ItemGroup Condition="'$(VisualStudioVersion)' < '15.0'">
  <None Include="app.config">
    <Link>$(TargetFileName).config</Link>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>

Have a look at this answer (based on work of @theyetiman). It requires mainly a modification of the project, the app.config file belongs to. The consuming projects are left unmodified.

<ItemGroup>
  <None Include="app.config" />
</ItemGroup>
<ItemGroup>
  <None Include="app.config">
    <Link>$(TargetFileName).config</Link>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>

Also note, that with Visual Studio 2017 this modification is not necessary any more. If you open a solution with a project containing the above workaround, a warning similar to the following is displayed in the errors window:

The file 'app.config' could not be added to the project.
Cannot add a link to the file ...\app.config.
This file is within the project directory tree.

Add a condition comparing $(VisualStudioVersion) to avoid this warning and keep backward compatibility:

<ItemGroup>
  <None Include="app.config" />
</ItemGroup>
<ItemGroup Condition="'$(VisualStudioVersion)' < '15.0'">
  <None Include="app.config">
    <Link>$(TargetFileName).config</Link>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文