Using NuGet.Core to Get the Latest Version of a Package

Add the NuGet.Core package from NuGet and you’ll be in business. We use a NuGet config-file to get the one or more repositories that you might be using and hit them one at a time.

using NuGet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.XPath;

namespace LatestVersion
{
    class PackageNotFoundException : Exception
    {
        public string PackageName { get; private set; }

        public PackageNotFoundException(string packageName) : base(String.Format("Package [{0}] not found.", packageName))
        {
            PackageName = packageName;
        }
    }

    class NuGet
    {
        string nugetConfigFilepath;

        public NuGet(string nugetConfigFilepath)
        {
            this.nugetConfigFilepath = nugetConfigFilepath;
        }

        public IEnumerable<Tuple> GetSources()
        {
            XPathNavigator nav;
            XPathDocument docNav;
            string xPath;

            docNav = new XPathDocument(nugetConfigFilepath);
            nav = docNav.CreateNavigator();
            xPath = "/configuration/packageSources/add";

            foreach (XPathNavigator xpn in nav.Select(xPath))
            {
                string name = xpn.GetAttribute("key", "");
                string uri = xpn.GetAttribute("value", "");

                yield return new Tuple(name, uri);
            }
        }

        public SemanticVersion GetLatestVersion(string packageName)
        {
            foreach (Tuple source in GetSources())
            {
                string name = source.Item1;
                string uri = source.Item2;

                IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository(uri);

                // Passing NULL for `versionSpec` will return the latest version.
                IEnumerable packagesRaw = repo.FindPackages(packageName, null, false, false);
                IList packages = packagesRaw.ToList();

                if (packages.Count == 0)
                {
                    continue;
                }

                return packages[0].Version;
            }

            throw new PackageNotFoundException(packageName);
        }
    }
}
Advertisements

Searching for Specific Packages in NuGet

Package searches in NuGet hit the query API and the query API, by default, does a substring search. This obviously might get annoying when a simple string that you’re searching for might appear within any of the searchable characteristics of many different packages.

However, since this is the standard query interface, and you can pass more than just flat strings, you can also modify the query to do an exact search using the “PackageId” modifier:

C:\>nuget list zebus
Zebus 1.4.6
Zebus.Directory 1.2.10
Zebus.Directory.Cassandra 1.2.10
Zebus.Directory.Standalone 1.2.10
Zebus.Persistence 1.0.2
Zebus.Persistence.CQL 1.0.2
Zebus.Persistence.CQL.Testing 1.0.2
Zebus.Testing 1.4.6

C:\>nuget list PackageId:zebus
Zebus 1.4.6