Search code examples
.netvisual-studiocsproj.net-framework-version

Stop Visual Studio from copying Nuget Packages in the output dir


Disclaimer: This is a .Net Framework specific problem, that doesn't happen in .Net Core.

I am currently wokring on a project, that works as an AddIn of a larger project. I'd like to set the output path of my project to the AddIn folder of the larger project then start the larger project with the debug-properties of my project, so I don't have to copy and attach to project each time manually.

Any reference I have is shared with and managed from the larger project, which means i don't have (and should not) copy them to the AddIn Folder.

I included code from this answer to my .csproj (VS 19/new format)

  <ItemDefinitionGroup>
  <Reference>
    <Private>False</Private>
  </Reference>
  <ProjectReference>
     <Private>False</Private>
  </ProjectReference>
  </ItemDefinitionGroup>

to exclude project- and regular references, but in .Net Framework all .dlls from my Nuget packages are also copied to the output path. I was looking for a way to disable that too, but haven't found one.

Is there a way to stop .dlls from Nuget packages to be copied to the output path in .Net Framework?


Solution

  • Take a look at this github post. Other than that, @Maytham covered it perfectly:

    why do not you publish nuget packages of the project to a AddIn? wont that help?

    He's right. That should help. But, just to recap the github post:

    You could turn off runtime access from every package:

    <ItemGroup>
        <PackageReference Include="Newtonsoft.Json" Version="12.0.1" ExcludeAssets="runtime" />
    </ItemGroup>
    

    Or you could do it for all packages:

    <ItemDefinitionGroup>
        <PackageReference ExcludeAssets="runtime" />
    </ItemDefinitionGroup>
    

    Once you have that, make a CopyNuGetDependencies (or whatever you want to call it) project with a different output path. Make sure it reference to all NuGet packages - but without ExcludeAssets. It's soley responsable for copying NuGet packages to the output directly.

    That should stop all of the issues.