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.
source share