如何以编程方式读取 packages.config 中的 NuGet 包列表?

How to read the list of NuGet packages in packages.config programatically?(如何以编程方式读取 packages.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?

在我们的源代码存储库中,我有很多解决方案和项目以及同样大量的 packages.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:fullpath	opackages.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:
ootpath", "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 包列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)