How to read the list of NuGet packages in program.config programmatically?

What is the best way to read (ideally through C #) the packages listed in packages.config files?

In our source code repository, I have many solutions and projects, as well as many package.config files. I am trying to create a summary list of packages (and versions) used in my source code repository.

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

thanks

+6
source share
2 answers

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

Here is an example of the code that this class uses to print out the package ID and 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); } 

You will need to find the packages.config files yourself, which you can probably do by searching the directory, for example:

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

An alternative and more modern way to do this is to install the NuGet.Packaging NuGet package and use code similar to the following:

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

As suggested, you will need to install NuGet.Core, your solution may have several projects, so it’s good to know how to specify the name of the project during installation. Let's say your solution is MySolution, and you have two projects Project01 and Project02 , and you want to install it only in Project02 .

Install-Package NuGet.Core -ProjectName Project02

Then you will need to add the using statement on whatever.cs page that you are going to do to target the package, and suppose you just want to get the version number so that you can print it somewhere on your Web site. This is what I wanted to do.

using NuGet;

next I wanted to get a specific package and read its version number so that when I release my software I would have a visual identifier in a specific place on my website where I can go and see the version that is being produced.

here is the code i wrote to populate the webforms label on my page.

 protected void Page_Load(Object sender, EventArgs e) { var pkgRefpath = Server.MapPath("~/packages.config"); PackageReferenceFile nugetPkgConfig = new PackageReferenceFile(pkgRefpath); IEnumerable<PackageReference> allPackages = nugetPkgConfig.GetPackageReferences(); var newtonsoftPkg = ( from pkg in allPackages where pkg.Id == "Newtonsoft.Json" select pkg ).FirstOrDefault(); if (newtonsoftPkg== null) return; var newtonsoftPkg_Version = newtonsoftPkg.Version; ltrNewtonsoftVer.Text = newtonsoftPkg_Version.ToString(); } 

This is a slightly different answer to the question, but it shows what solution I received for my needs after searching for this question / answer and changing what I learned to satisfy my own needs. Hope this helps someone else.

0
source

Source: https://habr.com/ru/post/1233337/


All Articles