I'm working on a project, and we need to know the version of a NuGet package we are using, and use it in the code.
One way I tried was to read from the packages.config file, parse it for the package I want, and parse the line for the version. Something like:
var lines = System.IO.File.ReadAllLines(@"..\..\packages.config");
foreach(var line in lines)
{
if (line.Contains("My.Package"))
{
foreach(var part in line.split(' '))
{
if (part.Contains("version"))
{
return part.Split('"')[1];
}
}
}
}
Is there a better way to do this programmatically?
Note: The above code will not work, as packages.config is not deployed with the application.