While programming if I need to get from a .NET app (not .NET Core app) the list of directories where NuGet stores its packages. Let’s see how to do this.
Next Option is:
In order to achieve this, you need to use the same code and libraries as nuget.exe does:
- Install nuget package NuGet.Configuration to your project.
- Add using NuGet.Configuration at the top of your file.
- Use the following code (which nuget.exe uses itself under the hood)
var settings = Settings.LoadDefaultSettings(null);
Console.WriteLine(SettingsUtility.GetGlobalPackagesFolder(settings));
Check More:
- Get TFS to ignore NuGet packages folder
- Unlocking files and folders locked by some employee on TFS
- How can I clear the NuGet package cache using the command line?
- How to get the nuget cache folder location programmatically
From within the .targets file, you can access the current file’s location using the $(MSBuildThisFileDirectory) property. Assuming the .targets file is in the nuget build folder; you could get to its own package folder using relative paths like this:
<PropertyGroup>
<ThisPackageDirectory>$(MSBuildThisFileDirectory)..\</ThisPackageDirectory>
...
I hope you enjoyed reading this post. For more information you can check out Microsoft learn.
For more information you can visit our C# Section for more such posts.