Gary Howlett
Gary Howlett

Reputation: 1417

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

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

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.

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

Thanks

Upvotes: 7

Views: 5533

Answers (2)

Matt Ward
Matt Ward

Reputation: 47987

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

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);
}

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...
}

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);
}

Upvotes: 16

Eric Bishard
Eric Bishard

Reputation: 5341

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

Install-Package NuGet.Core -ProjectName Project02

Next you will need to add a using statement in the whatever.cs page you are going to do your work to target the package and let's say you just want to get the version number so that you can print it out somewhere on your website. That is actually what I wanted to do.

using NuGet;

next I wanted to get at a specific package and read it's version number so that when we release my software I have a visual identifier at a certain place on my website that I can go to and see the version that is in production.

here is the code I wrote to populate a 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 this shows the solution that I ended up with for my needs after finding this Question/Answer and modifying what I learned to suit my own needs. I hope it can help someone else out.

Upvotes: 0

Related Questions