Set content files to "copy local : always" in a nuget package

I have made this which copies files from my build folder to the output folder (bin/debug or bin/release). Works like a charm for me.

Nuspec file:

<package>
  <files>
    <file src="\bin\Release\*.dll" target="lib" />
    <file src="\bin\Release\x64\*.dll" target="build\x64" />
    <file src="\bin\Release\x86\*.dll" target="build\x86" />
    <file src="MyProject.targets" target="build\" />    
  </files>
</package>

MyProject.targets

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <NativeLibs Include="$(MSBuildThisFileDirectory)**\*.dll" />
    <None Include="@(NativeLibs)">
      <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

You can use PowerShell and the Install.ps1 hook provided by NuGet.

See the documentation.

Via PowerShell you have to 'search' for the content element which includes your importantfile.xml in an attribute. When the script found it, it has to add <CopyToOutputDirectory>Always</CopyToOutputDirectory> as a child element.

    <Content Include="importantfile.xml">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>

You can find some PowerShell snippets here. Just take a look at the .ps1 files.

You could try the following (not tested). The file has to be named Install.ps1 and copied into the tools folder:

param($installPath, $toolsPath, $package, $project)

# Load project XML.
$doc = New-Object System.Xml.XmlDocument
$doc.Load($project.FullName)
$namespace = 'http://schemas.microsoft.com/developer/msbuild/2003'

# Find the node containing the file. The tag "Content" may be replace by "None" depending of the case, check your .csproj file.
$xmlNode = Select-Xml "//msb:Project/msb:ItemGroup/msb:Content[@Include='importantfile.xml']" $doc -Namespace @{msb = $namespace}


#check if the node exists.
if($xmlNode -ne $null)
{
    $nodeName = "CopyToOutputDirectory"

    #Check if the property already exists, just in case.
    $property = $xmlNode.Node.SelectSingleNode($nodeName)
    if($property -eq $null)
    {
        $property = $doc.CreateElement($nodeName, $namespace)
        $property.AppendChild($doc.CreateTextNode("Always"))
        $xmlNode.Node.AppendChild($property)

        # Save changes.
        $doc.Save($project.FullName)
    }
}

You should also check if everything is removed completely when uninstalling the package.

Note by Jonhhy5

When updating the package via update-package, Visual Studio warns that the project is modified "outside the environnment". That's caused by $doc.Save($project.FullName). If I click reload before the command is fully terminated, it sometimes causes errors. The trick is to leave the dialog there until the process finishes, and then reload the projects.


I know you guys got a working solution to this but it didn't work for me so I'm going to share what I pulled out of the NLog.config NuGet package install.ps1 (github source here).

NOTE: this is not my code, this is the content of the install.ps1 from the NLog.config nuget package just sharing the knowledge.

It seems a little more straight forward to me and just hoping to help others that will likely stumble upon this.

You can find the accepted int values for BuildAction here and the accepted values for CopyToOutputDirectory here.

if the link breaks again enter image description here

Fields prjBuildActionCompile 1
The file is compiled.

prjBuildActionContent 2
The file is included in the Content project output group (see Deploying Applications, Services, and Components)

prjBuildActionEmbeddedResource 3
The file is included in the main generated assembly or in a satellite assembly as a resource.

prjBuildActionNone 0
No action is taken.

param($installPath, $toolsPath, $package, $project)

$configItem = $project.ProjectItems.Item("NLog.config")

# set 'Copy To Output Directory' to 'Copy if newer'
$copyToOutput = $configItem.Properties.Item("CopyToOutputDirectory")

# Copy Always Always copyToOutput.Value = 1
# Copy if Newer copyToOutput.Value = 2  
$copyToOutput.Value = 2

# set 'Build Action' to 'Content'
$buildAction = $configItem.Properties.Item("BuildAction")
$buildAction.Value = 2

Instead of using a PowerShell script another approach is to use an MSBuild targets or props file with the same name as the package id:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="$(MSBuildThisFileDirectory)importantfile.xml">
      <Link>importantfile.xml</Link>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

In the nuspec file then, instead of adding the required files to the Content directory, add them to the Build directory along with the targets file.

  • Build
    • importantfile.xml
    • MyPackage.targets
  • lib
    • net45
      • MyAssembly.dll

If you require different content for different architectures then you can add architecture folders under Build also each with their own targets file.

Benefits to using a targets file over the PowerShell script with NuGet Content directory:

  • required content files aren't shown in the project in Visual Studio
  • content files are linked to rather than copied into the directory of each project which references the NuGet package (preventing there being multiple copies and keeping behaviour the same as for assemblies / libraries from NuGet packages)
  • PowerShell scripts only work in Visual Studio and aren't run when NuGet is run from the commandline (build servers, other IDEs and other OS), this approach will work everywhere
  • PowerShell install scripts are not supported in NuGet 3.x project.json system.