简体   繁体   English

如何以编程方式获取nuget缓存文件夹位置

[英]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. 我需要从.NET应用程序(不是.NET Core应用程序)获取NuGet存储其包的目录列表。

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. 我知道默认情况下它位于C:\\Users\\[YourUsername]\\.nuget\\packages因此选项是迭代所有用户,假设运行该进程的当前Windows用户拥有访问其他用户数据的权限。 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. 但它不是.NET代码,根据答案它似乎是一个过时的选项。


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: 此外,我想了解.NET Core NuGet包位置的逻辑,因为我可以在3个位置看到一些.NET Core包:

  • C:\\Program Files\\dotnet\\shared\\Microsoft.NETCore.App (don't contain AspNetCore packages) C:\\Program Files\\dotnet\\shared\\Microsoft.NETCore.App (不包含AspNetCore包)
  • C:\\Users\\[UserName]\\.nuget\\packages (do contain AspNetCore packages) C:\\Users\\[UserName]\\.nuget\\packages (包含AspNetCore包)
  • C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages (do contain AspNetCore packages) C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages (包含AspNetCore包)

In order to achieve this you need to use the same code and libraries as nuget.exe does: 为了实现这一点,您需要使用与nuget.exe相同的代码和库:

  1. Install nuget package NuGet.Configuration to your project 将nuget包NuGet.Configuration安装到您的项目中
  2. Add using NuGet.Configuration at the top of your file using NuGet.Configuration文件顶部的using NuGet.Configuration添加
  3. Use the following code(which nuget.exe uses itself under the hood): 使用以下代码( nuget.exe在引擎盖下使用它):
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 您还可以使用ILSpy检查nuget.exe以了解它是如何工作的,或者在github NuGet / NuGet.Client上使用相关的源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM