5

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?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

1 Answers1

5

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.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • Thanks for your answer. Is the `PackageReference` a VS 2017 thing only? In older versions `packages.config` is only ever used, right? (maybe unless you explicitly install never version NuGet addon). Also, it seems that with `PackageReference`, the `install.ps1` is not executed, is that correct? – Martin Prikryl Nov 24 '17 at 08:15
  • I've posted a follow-up question [Prevent duplicating files in NuGet content and contentFiles folders](https://stackoverflow.com/q/47468941/850848). Can you take a look? – Martin Prikryl Nov 24 '17 at 08:20