How can I set the 'copy to output directory' property in my nuspec file?

How can I set the 'copy to output directory' property in my nuspec file?

Martin pointed out the right direction, I have same request before and kjbartel`s answer is nice to me. I post the answer here with more detail for you question, hope this can give you some help.

To resolve this question, you can follow below steps:

  1. Add a xx.targets file in your project folder, make sure the name of the target file is the same name as the package id(TestDemo is my package ID, so the name of .targets is TestDemo.targets).

  2. Add below code in the targets file:

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

Note: The path of "$(MSBuildThisFileDirectory)" should be relative path, if you are not familiar with it, you can use the absolute path.

  1. In the nuspec file, add required file to the Build directory along with the targets file.

      <files>
        <file src="bin\x64\Debug\GR*.txt" target="Build\" />
        <file src="TestDemo.targets" target="Build\" />
        <file src="bin\Debug\TestDemo.dll" target="lib\462" />
      </files>
    
  2. Pack this package, then add it on other project to test, it work fine.


The accepted answer will be useful for the non-content files as it wont be linked to project when installed.

However, I had a requirement to have a settings xml file which will be added to the project and the nuget package user can edit it and package dll will load the edited xml file from output directory.

Since content files wont be copied to build directory, I had to use .targets file to copy content file to output directory.

nuspec file

<file src="TestDemo.targets" target="build"/>
<file src="Settings.xml" target="content/Configuration"/>

.targets file (file name has to be same as package id)

<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="AfterBuild">
    <Copy SourceFiles="$(ProjectDir)Configuration\Settings.xml" DestinationFolder="$(TargetDir)Configuration\" ContinueOnError="true" />
  </Target>
</Project>