NuGet 还原不会复制配置文件



我的.netstandard2.0包在nuget包中打包了一个配置文件,它包含以下指令:

<ItemGroup>
    <None Include="apps.config" Pack="True" PackagePath="lib/$(TargetFramework)">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
   </None>
</ItemGroup>
使用 nuget 包

资源管理器,我可以看到该文件确实打包在 nuget 包中,放在与 .dll 文件相同的目录下。

然后我使用 install-package 命令将此包添加到另一个项目中,但在运行应用程序时,只有.dll文件复制到 bin\debug 中,而不是配置文件中。执行还原时如何复制配置文件?

使用 NuGet 的contentFiles功能包含项目要使用的内容。如果将文件打包为Content项而不是None并添加PackageCopyToOutput="true"元数据,NuGet 将创建正确的路径和 nuspec 内容:

<ItemGroup>
  <None Remove="apps.config" />
  <Content Include="apps.config" Pack="true" PackageCopyToOutput="true" />
</ItemGroup>