03 June 2015 NuGet Robert Muehsig

The typical use cases for NuGet may involve the NuGet Client inside Visual Studio or the NuGet.exe, but of course you can use the same functionality via code. NuGet - as a package format - can be used for many different things, for example Chocolatey and Squirrel are using NuGet to distribute and update software.

Getting started with the NuGet API

All you need is the NuGet.Core NuGet-Package. This package seems to contain the logic to create, read and load packages, which is (more or less) the most interesting part of a Package Manager. So, let’s take a deeper look.

Create a NuGet Package via Code

A NuGet-Package is basically a Zip-Archive, but with Metadata. When you create a library and ship it via NuGet you will need to write a .nuspec file. Well… with the help of the NuGet.Core package we can define the Metadata with code and harvest the desired content/tools/lib etc. files:

ManifestMetadata metadata = new ManifestMetadata()
{
    Authors = "Authors Name",
    Version = "1.0.0.0",
    Id = "NuGetId",
    Description = "NuGet Package Description goes here!",
};

PackageBuilder builder = new PackageBuilder();

var path = AppDomain.CurrentDomain.BaseDirectory + "..\\..\\DemoContent\\";

builder.PopulateFiles(path, new[] { new ManifestFile { Source = "**", Target = "content" } });
builder.Populate(metadata);

using (FileStream stream = File.Open("test.nupkg", FileMode.OpenOrCreate))
{
    builder.Save(stream);
}

The first lines defines the NuGet Metadata, then we just collect files in a folder called “DemoContent”, which is included in the sample.

x

Read NuGet Packages via Code

Reading NuGet-Package is really easy and the NuGet.Core Package has some nice utility classes to extract the content of the actual package:

NuGet.ZipPackage package = new ZipPackage("test.nupkg");
var content = package.GetContentFiles();

// extract content like content.First().GetStream();

Console.WriteLine("Package Id: " + package.Id);

My Complete Sample

The sample code is also available on GitHub

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Create NuGet Package via Code");
        ManifestMetadata metadata = new ManifestMetadata()
        {
            Authors = "Authors Name",
            Version = "1.0.0.0",
            Id = "NuGetId",
            Description = "NuGet Package Description goes here!",
        };

        PackageBuilder builder = new PackageBuilder();

        var path = AppDomain.CurrentDomain.BaseDirectory + "..\\..\\DemoContent\\";

        builder.PopulateFiles(path, new[] { new ManifestFile { Source = "**", Target = "content" } });
        builder.Populate(metadata);

        using (FileStream stream = File.Open("test.nupkg", FileMode.OpenOrCreate))
        {
            builder.Save(stream);
        }

        Console.WriteLine("... and extract NuGet Package via Code");

        NuGet.ZipPackage package = new ZipPackage("test.nupkg");
        var content = package.GetContentFiles();

        Console.WriteLine("Package Id: " + package.Id);
        Console.WriteLine("Content-Files-Count: " + content.Count());

        Console.ReadLine();
    }
}

x

Using NuGet not only for assemblies…

As already mentioned: NuGet can be used for all kinds of “packaged content”. The good part of using NuGet is that you can use a wide range of tooling around it - like the NuGet Package Explorer or the NuGet.Core library.

Happy coding!


Written by Robert Muehsig

Software Developer - from Dresden, Germany, now living & working in Switzerland. Microsoft MVP & Web Geek.
Other Projects: KnowYourStack.com | ExpensiveMeeting | EinKofferVollerReisen.de

If you like the content and want to support me you could buy me a beer or a coffee via Litecoin or Bitcoin - thanks for reading!