23

I need to get from a .NET app (that is not .NET Core app) the list of directories where NuGet stores its packages.

I know by default it is in C:\Users\[YourUsername]\.nuget\packages so an option is to iterate over all users assuming the current Windows user that runs the process owns the right to access other users data. Moreover if this default dir is changed by the user I am stuck.

An answer is provided here through

nuget locals packages-cache -list

but it is not .NET code and it seems to be an obsolete option according to the answer.


Moreover I'd like to understand the logic of .NET Core NuGet packages locations, because I can see some .NET Core packages in 3 locations:

  • C:\Program Files\dotnet\shared\Microsoft.NETCore.App (don't contain AspNetCore packages)
  • C:\Users\[UserName]\.nuget\packages (do contain AspNetCore packages)
  • C:\Program Files (x86)\Microsoft SDKs\NuGetPackages (do contain AspNetCore packages)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
  • See https://learn.microsoft.com/en-us/dotnet/core/install/how-to-detect-installed-versions?pivots=os-windows#check-runtime-versions dotnet --list-runtimes For the items in this folder C:\Program Files\dotnet\shared\Microsoft.NETCore.App – Ian Apr 22 '21 at 13:43

1 Answers1

13

In order to achieve this you need to use the same code and libraries as nuget.exe does:

  1. Install nuget package NuGet.Configuration to your project
  2. Add using NuGet.Configuration at the top of your file
  3. Use the following code(which nuget.exe uses itself under the hood):
var settings = Settings.LoadDefaultSettings(null);
Console.WriteLine(SettingsUtility.GetGlobalPackagesFolder(settings));

You also can check nuget.exe with ILSpy to figure out how it works or use relevant source code at github NuGet/NuGet.Client

Igor B
  • 905
  • 11
  • 26