Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the nuget cache folder location programmatically

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)
like image 555
Patrick from NDepend team Avatar asked Jul 26 '17 13:07

Patrick from NDepend team


People also ask

Where are NuGet files stored?

By default, all NuGet clients (the command-line tool, the Visual Studio extension and the Package Manager Console) all make use of the default NuGet configuration file which lives under %AppData%\NuGet\NuGet. config.

Do I need .NuGet folder?

nuget folder is used as a cache for packages downloaded to speed up project restore and compilation. It can safely be removed. Worst case, it will have to download the packages again in the future. Save this answer.


1 Answers

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

like image 146
Igor B Avatar answered Sep 27 '22 22:09

Igor B