25

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)
1

1 Answer 1

15
+250

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

1
  • We won't have the time to test this right now but it sounds good and will post about my experience trying this. Meantime I check this question as answered thanks Commented Jul 31, 2017 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.