When i publish my project, files included with a nuget package are not copied

Omar 20 Reputation points
2025-01-17T10:41:13.2466667+00:00

I have a project using a custom nuget package.

The nuget package uses some dlls, added to the project with property "Copy to output directory" set to "Copy always".

When I install the nuget package in the main project the dlls are added to it, but property "Copy to output directory" is set to "Do not copy".

Consequantly, when I publish my project, necessary files are missing.

To solve this, I have to manually set the property "Copy to output directory" to "Copy always" each time i install my nuget package or I clone the main project from the GIT repository.

Is there a way to maintain the value of the property "Copy to output directory" of files copied from a nuget package?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,093 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,280 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 48,866 Reputation points Microsoft Vendor
    2025-01-20T07:28:18.4666667+00:00

    Hi @Omar , Welcome to Microsoft Q&A,

    Add a .targets file, such as MyPackage.targets, to the build or buildTransitive folder of your NuGet package with the following content:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <ItemGroup>
        <!-- Set CopyToOutputDirectory for a specific DLL in the package -->
        <Content Include="$(MSBuildThisFileDirectory)..\runtimes\win\lib\netstandard2.0\YourDependency.dll">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    </Project>
    

    $(MSBuildThisFileDirectory) refers to the directory where the current .targets file is located and is used to resolve the path dynamically.

    If the DLL exists in a specific package path, such as runtimes/win/lib/netstandard2.0/YourDependency.dll, adjust the path to match the actual location. Make sure the .targets file is correctly embedded in the NuGet package

    The directory structure of the NuGet package is as follows:

    /build/
        MyPackage.targets
    /lib/
        netstandard2.0/
            YourDependency.dll
    

    Rebuild the NuGet package and reinstall it in your project.


    A reference solution:MSBuild doesn't copy references (DLL files) if using project dependencies in solution

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.