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

Ham*_*thi 13 c# nuget .net-core

在过去使用.NET Framework时,我使用此示例以编程方式使用nuget

以编程方式玩包!

是否有任何等效的.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"));
Run Code Online (Sandbox Code Playgroud)

我想以编程方式下载并安装任何软件包.

https://api.nuget.org/v3/index.json
Run Code Online (Sandbox Code Playgroud)

nat*_*ter 9

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

在docs.microsoft.com上查看NuGet API v3以获取有关NuGet 3的信息.在撰写本文时,这个文档基本上是一个很大的TODO并且没有太多信息.

以下是一些更有用的博客文章.

探索NuGet v3库,第1部分简介和概念

探索NuGet v3库,第2部分

探索NuGet v3库,第3部分

当然,你总是可以通过NuGet的源代码来探索更多的例子.大多数核心逻辑都存在于https://github.com/nuget/nuget.client中.