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)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.
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.
In order to achieve this you need to use the same code and libraries as nuget.exe
does:
NuGet.Configuration
to your projectusing NuGet.Configuration
at the top of your filenuget.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With