Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the NuGet package version programmatically from a NuGet feed?

Tags:

c#

nuget

First off, of all the NuGet code, I'm trying to figure out which one to reference.

The main question is, given a NuGet package name, is there a programmatic way to retrieve the versions from the NuGet feed and also the latest version for general consumption?

For example, given a package name of ILMerge, it would be nice to get the latest package version of 2.13.307.

// Pseudo code, makes a lot of assumptions about NuGet programmatic interfaces
PackageRef currentVersion = nugetlib.getpackageinfo(args[0]);
Console.WriteLine("Package Id: '{0}':", pkg.Id);
Console.WriteLine("  Current version: {0}", pkg.Version);
Console.WriteLine("  Available versions: {0}", String.Join(",",pkg.Versions.Select(_=>_)));
like image 615
enorl76 Avatar asked Oct 31 '14 15:10

enorl76


People also ask

How do I find the NuGet package version?

In Visual Studio, use the Help > About Microsoft Visual Studio command and look at the version displayed next to NuGet Package Manager. Alternatively, launch the Package Manager Console (Tools > NuGet Package Manager > Package Manager Console) and enter $host to see information about NuGet including the version.

How do I change NuGet package version?

Right-click the Packages folder in the project, and select Update. This will update the NuGet package to the latest version. You can double-click the Add packages and choose the specific version.


2 Answers

Use the NuGet core package:

string packageID = "ILMerge";

// Connect to the official package repository
IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");
var version =repo.FindPackagesById(packageID).Max(p=>p.Version);

Reference: Play with Packages, programmatically!

like image 93
Damith Avatar answered Sep 20 '22 21:09

Damith


There exist a quite nice NuGet API to accomplish both.

a) Retrieving the NuGet package versions (for the Newtonsoft.Json package in following example):

GET https://api.nuget.org/v3-flatcontainer/Newtonsoft.Json/index.json

b) Downloading a certain version of the NuGet package - e.g.

GET https://api.nuget.org/v3-flatcontainer/utf8json/1.3.7/utf8json.1.3.7.nupkg

Please try to copy the URL to the browser and you could see the results of the examples...

To get more information about API, please visit the Package Content

The following code example could read the versions of a package (using the Microsoft.AspNet.WebApi.Client NuGet package which provides HttpContent's ReadAsAsync<T> extension for parsing the JSON result to an appropriate class - the VersionsResponse in this example)

using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace GetNuGetVer
{
    class VersionsResponse
    {
        public string[] Versions { get; set; }
    }

    class Program
    {
        static async Task Main(string[] args)
        {
            var packageName = "Enums.NET";
            var url = $"https://api.nuget.org/v3-flatcontainer/{packageName}/index.json";
            var httpClient = new HttpClient();
            var response = await httpClient.GetAsync(url);
            var versionsResponse = await response.Content.ReadAsAsync<VersionsResponse>();
            var lastVersion = versionsResponse.Versions[^1]; //(length-1)
            // And so on ..
        }
    }
}

To get rid of Microsoft.AspNet.WebApi.Client (with dependency to Newtonsoft.Json), System.Text.Json can be used out of the box (since .NET Core 3.0) with code changes as follows:

        using System.Text.Json;
        ...
        var response = await httpClient.GetAsync(url);
        var versionsResponseBytes = await response.Content.ReadAsByteArrayAsync();
        var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
        var versionsResponse = JsonSerializer.Deserialize<VersionsResponse>(versionsResponseBytes, options);
        var lastVersion = versionsResponse.Versions[^1]; //(length-1)

or just a different JSON parser based on your own preferences.

like image 20
rychlmoj Avatar answered Sep 17 '22 21:09

rychlmoj