gpt4 book ai didi

c# - 如何在 .NET Core 中以编程方式从 nuget 下载 nupkg 包?

转载 作者:可可西里 更新时间:2023-11-01 08:26:15 29 4
gpt4 key购买 nike

在过去的 .NET Framework 中,我使用这个示例以编程方式使用 nuget

Play with Packages, programmatically!

是否有 .NET Core 的等效源?

//ID of the package to be looked up
string packageID = "EntityFramework";

//Connect to the official package repository
IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");

//Get the list of all NuGet packages with ID 'EntityFramework'
List<IPackage> packages = repo.FindPackagesById(packageID).ToList();

//Filter the list of packages that are not Release (Stable) versions
packages = packages.Where (item => (item.IsReleaseVersion() == false)).ToList();

//Iterate through the list and print the full name of the pre-release packages to console
foreach (IPackage p in packages)
{
Console.WriteLine(p.GetFullName());
}

//---------------------------------------------------------------------------

//ID of the package to be looked up
string packageID = "EntityFramework";

//Connect to the official package repository
IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");

//Initialize the package manager
string path = <PATH_TO_WHERE_THE_PACKAGES_SHOULD_BE_INSTALLED>
PackageManager packageManager = new PackageManager(repo, path);

//Download and unzip the package
packageManager.InstallPackage(packageID, SemanticVersion.Parse("5.0.0"));

我想以编程方式下载和安装任何包。

https://api.nuget.org/v3/index.json

最佳答案

您显示的代码示例使用 .NET Core 不支持的 NuGet 2。您需要使用 NuGet 3 或(即将发布的)NuGet 4。这些 API 与 NuGet 2 有很大的不同。其中一个重大变化是 NuGet.Core已过时,不会移植到 .NET Core。

结帐 NuGet API v3在 learn.microsoft.com 上获取有关 NuGet 3 的信息。在撰写本文时,该文档基本上是一个很大的 TODO,没有太多信息。

这里有一些更有用的博客文章。

Exploring the NuGet v3 Libraries, Part 1Introduction and concepts

Exploring the NuGet v3 Libraries, Part 2

Exploring the NuGet v3 Libraries, Part 3

当然,您始终可以深入研究 NuGet 的源代码以找到更多示例。大多数核心逻辑都在 https://github.com/nuget/nuget.client 中.

关于c# - 如何在 .NET Core 中以编程方式从 nuget 下载 nupkg 包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42174699/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com