title: Copy files from Nuget package to output directory with MsBuild in .csproj and dotnet pack command
body: <p>Last time I had to find out how to extract some files from a Nuget package in took me at least 6 months but I finally managed to find <a href="https://stackoverflow.com/a/40652794/1203116">the solution</a>.</p>
  
  <p>The thing is that, this solution assumes I have a <code>.nupkg</code> file and manually add a <code>.targets</code> file to perform the extraction process.</p>
  
  <p>Now, things are different:</p>
  
  <ol>
  <li>I don't have any <code>.nupgk</code> file, we generate one automatically on our VSTS server using the <code>dotnet pack</code> command. Then we consume the package from our Nuget server</li>
  <li>We can't afford to take another 6 months to find the solution</li>
  </ol>
  
  <p>Here is my <code>ProjectName.csproj</code></p>
  
  <pre><code>&lt;Project Sdk="Microsoft.NET.Sdk"&gt;
      &lt;PropertyGroup&gt;
          &lt;TargetFramework&gt;netstandard2.0&lt;/TargetFramework&gt;
          &lt;RestoreProjectStyle&gt;PackageReference&lt;/RestoreProjectStyle&gt;
          &lt;Authors&gt;Jérôme MEVEL&lt;/Authors&gt;
          &lt;Version&gt;1.0.3&lt;/Version&gt;
          &lt;GeneratePackageOnBuild&gt;true&lt;/GeneratePackageOnBuild&gt;
  
          &lt;!-- This answer got many votes on a Github thread so I tried just in case --&gt;
          &lt;CopyLocalLockFileAssemblies&gt;true&lt;/CopyLocalLockFileAssemblies&gt;
  
          &lt;!-- Just trying that too--&gt;
          &lt;IncludeBuildOutput&gt;true&lt;/IncludeBuildOutput&gt;
          &lt;IncludeContentInPack&gt;true&lt;/IncludeContentInPack&gt;
  
          &lt;!-- I wanted to see the full generated Nuget package --&gt;
          &lt;IncludeSource&gt;true&lt;/IncludeSource&gt;
  
          &lt;!-- desperate attempt --&gt;     
          &lt;TargetsForTfmSpecificBuildOutput&gt;GetMyPackageFiles&lt;/TargetsForTfmSpecificBuildOutput&gt;
      &lt;/PropertyGroup&gt;
      &lt;ItemGroup&gt;
          &lt;PackageReference Include="Dapper" Version="1.50.5" /&gt;
          &lt;PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" /&gt;
          &lt;PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" /&gt;
          &lt;PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" /&gt;
          &lt;PackageReference Include="NLog" Version="4.5.8"&gt;
  
              &lt;!-- Let's try this too just in case--&gt;
              &lt;IncludeAssets&gt;all&lt;/IncludeAssets&gt;
          &lt;/PackageReference&gt;
          &lt;PackageReference Include="NLog.Extensions.Logging" Version="1.2.1"&gt;
              &lt;IncludeAssets&gt;all&lt;/IncludeAssets&gt;
          &lt;/PackageReference&gt;
          &lt;PackageReference Include="NLog.Web.AspNetCore" Version="4.6.0"&gt;
              &lt;IncludeAssets&gt;all&lt;/IncludeAssets&gt;
          &lt;/PackageReference&gt;
          &lt;PackageReference Include="System.Data.Common" Version="4.3.0" /&gt;
          &lt;PackageReference Include="System.Data.SqlClient" Version="4.5.1" /&gt;
      &lt;/ItemGroup&gt;
      &lt;ItemGroup&gt;
  
          &lt;!-- Added the file to the root folder in case &lt;IncludeAssets&gt;all&lt;/IncludeAssets&gt; is looking there --&gt;
          &lt;Content Include="NLog.config"&gt;
              &lt;Pack&gt;true&lt;/Pack&gt;
              &lt;PackagePath&gt;NLog;;&lt;/PackagePath&gt;
              &lt;CopyToOutputDirectory&gt;Always&lt;/CopyToOutputDirectory&gt;
          &lt;/Content&gt;
  
          &lt;!-- My desired path to look for the file --&gt;
          &lt;Content Include="NLog\NLog.config"&gt;
            &lt;Pack&gt;true&lt;/Pack&gt;
            &lt;PackagePath&gt;NLog;;&lt;/PackagePath&gt;
            &lt;CopyToOutputDirectory&gt;Always&lt;/CopyToOutputDirectory&gt;
          &lt;/Content&gt;
      &lt;/ItemGroup&gt;
  
      &lt;!-- This is the default setting, perfectly working when I reference the project instead of installing the Nuget package --&gt;
      &lt;ItemGroup&gt;
          &lt;None Update="NLog\NLog.config"&gt;
              &lt;CopyToOutputDirectory&gt;Always&lt;/CopyToOutputDirectory&gt;
          &lt;/None&gt;
      &lt;/ItemGroup&gt;
  
      &lt;!-- desperate attempt --&gt;
      &lt;Target Name="GetMyPackageFiles"&gt;
          &lt;ItemGroup&gt;
              &lt;BuildOutputInPackage Include="NLog/Nlog.config"&gt;
                  &lt;TargetPath&gt;NLog&lt;/TargetPath&gt;
              &lt;/BuildOutputInPackage&gt;
          &lt;/ItemGroup&gt;
      &lt;/Target&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <p>As you can see I tried several different settings. This MsBuild results in a <code>NLog.config</code> file included in a <code>NLog</code> folder at the root of the Nuget package file.</p>
  
  <p><a href="https://i.stack.imgur.com/qovWk.png" rel="noreferrer"><img src="https://i.stack.imgur.com/qovWk.png" alt="enter image description here"></a></p>
  
  <p>During my different tries, depending of the configuration I set, I was able to end-up with the <code>NLog.config</code> file at <code>src/ProjectName.Logging/NLog/NLog.config</code> or even at <code>lib/netstandard2.0/Nlog.config</code>.</p>
  
  <p>So my file is definitely included in my Nuget package file but isn't copied in the output directory of the project that consumes the package.</p>
  
  <p>I tried to specify a <code>.nuspec</code> file when generating my package with <code>dotnet pack</code> like <a href="https://docs.microsoft.com/en-us/nuget/reference/msbuild-targets#packing-using-a-nuspec" rel="noreferrer">described here</a> but I was never able to get a desired result (either only my <code>NLog.config</code> was included in the Nuget package or all the source files). Moreover, this has several downsides like overriding the configuration in the <code>.csproj</code> file or adding useless complexity. I believe what I want to achieve could be done without using a <code>.nuspec</code> file (maybe I'm wrong).</p>
  
  <p>I noticed the <code>build/ProjectName.targets</code> file is missing in my package and this is probably the missing piece. So how to add this <code>.targets</code> file without manually modifying the package?</p>
  
  <p>Is there another way to copy my config file out of the Nuget package to the output directory? </p>
  
  <p>I really hope someone could help me solve this issue. This is the 2nd time I want to perform the same operation but with a slight difference and once again this is hard to do.</p>
  
  <p>Thanks a lot</p>