问题描述
读取(最好是通过 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 包列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!