Use NuGet contentFiles to copy a file to build output directory

Viewed 5366

I need my NuGet package to add a file to a project and set its "Copy to Output Directory" flag.

So far, I'm using install.ps1 script, as seen in the answer by @workabyte to Set content files to “copy local : always” in a nuget package.

But I've understood that NuGet 3.3 and newer has a native support using its contentFiles model.

My understanding was that I create a directory structure like:

contentFiles\
  any\
    any\
      image.png

And then in my .nuspec:

<?xml version="1.0"?>
<package>
  <metadata minClientVersion="3.3.0">
    ...
    <contentFiles>
      <files include="**/*.png" buildAction="None" copyToOutput="true" />
    </contentFiles>
  </metadata>
</package>

When the package is compiled, the image.png with its directory structure is added to the .nupkg.

But, when I install the package in Visual Studio (2017), the image.png is not even added to the project.

What am I doing wrong?

1 Answers

The contentFiles feature is supported only when using the PackageReference "style" of referencing NuGet packages. It does not work for projects using packages.config (not that there is no migration path at the time of writing other than uninstalling all packages, changing the type and installing all of them again).

See NuGet's blog post "NuGet is now fully integrated into MSBuild", especially the section "What about other project types that are not .NET Core?" for instructions on how to set the preferred way of referencing NuGet packages.

Related