config中的NuGet软件包列表

config中的NuGet软件包列表

本文介绍了如何以编程方式读取packages.config中的NuGet软件包列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

读取(最好通过C#)packages.config文件中列出的软件包的最佳方法是什么?

What's the best way to read (ideally via C#) the packages listed in packages.config files?

在我们的源代码存储库中,我有很多解决方案和项目,同样还有很多package.config文件.我正在尝试构建在我的源代码存储库中使用的软件包(和版本)的合并列表.

Within our source code repository I have a lot of solutions and projects and equally a lot of packages.config files. I'm trying to build a consolidated list of packages (and versions) in use across my source code repository.

我看到有一个可用的NuGet.Core软件包-如何使用它来实现我的目标?

I can see there is a NuGet.Core package available - how could I use this to achieve my goal?

谢谢

推荐答案

如果您不想直接读取XML,则可以安装NuGet.Core NuGet程序包,然后使用PackageReference类.

If you do not want to read the XML directly you can install the NuGet.Core NuGet package and then use the PackageReference class.

下面是一些使用该类打印出程序包ID及其版本的示例代码.

Here is some example code that uses this class to print out the package id and its version.

string fileName = @"c:\full\path\to\packages.config";

var file = new PackageReferenceFile(fileName);
foreach (PackageReference packageReference in file.GetPackageReferences())
{
    Console.WriteLine("Id={0}, Version={1}", packageReference.Id, packageReference.Version);
}

您将需要自己找到packages.config文件,您可以通过目录搜索来完成,例如:

You will need to find the packages.config files yourself which you can probably do with a directory search, something like:

foreach (string fileName in Directory.EnumerateFiles("d:\root\path", "packages.config", SearchOption.AllDirectories))
{
    // Read the packages.config file...
}

另一种更新方法是安装NuGet.Packaging NuGet软件包并使用类似于以下代码:

An alternative and more up to date way of doing this is to install the NuGet.Packaging NuGet package and use code similar to:

var document = XDocument.Load (fileName);
var reader = new PackagesConfigReader (document);
foreach (PackageReference package in reader.GetPackages ())
{
    Console.WriteLine (package.PackageIdentity);
}

这篇关于如何以编程方式读取packages.config中的NuGet软件包列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:51