This is still active and controversial after 8 years so the
BIG UPDATE
To know your package you need to read the packages from your project file. It could be a packages.config
setup or package references. You shouldn't read from packages.config
because this file is often out of sync. You should read Hint
and PackageReference
tags in the project file. And if your solution has multiple projects? One way - use Pre Build Event
and a tool that you can write using PowerShell. Your input will be the project file, in which you can find tags, and then parse the digits out of them.
Then you create an output - a file where you write a class and in that class a dictionary with package name and version. In fact, you can make a list and not parse the version out of the name. You can also have a template saved and only replace and re-save the part. so, save the template in the project like
using System;
using System.Collections.Generic;
namespace Packages.Versions
{
public class PackageCollection
{
public PackageCollection ()
{
Packages = new List<string>();
//PLACEHOLDER
}
public List<string> Packages { get; }
}
}
And in the PowerShell you compose a string like
"Packages.Add(\"MyPackage.Name.123.0.0\");
Packages.Add(\"MyPackage2.Name.987.0.0\");
Packages.Add(\"MyPackage3.Name.567.0.0\");"
And then you plug this into the //PLACEHOLDER
and overwrite the file.
This will give you more precise result of what your project(s) use but not the dependencies of the packages themselves.
However!! The packages that are referenced at the design/compile time do not guarantee that these are the same ones used at runtime (and I am talking about DLLs in the packages). Different projects in your solution can reference different versions of the same package and only 1 version will be used (usually, because it is possible to have multiple versions.)
This is why I said below that answer to "how to collect package version" can actually mislead the whole operation. Because you might want to get Assembly Version and File Version rather than package version. These you can get reliably. And I have the links below.
END big update
Original answer below
Good question. But you have not said why you want to know the version.
I have to start my answer from pointing that you're on the wrong path. I suspect that you need to know at runtime the version of assembly in use.
But the whole NuGet concept and packages.config
is a compile-time concept and not a runtime concept. You shouldn't even have packages.config
in your output folder or published folder. At runtime you need to get loaded assemblies from AppDomain.CurrentDomain
. I will not go in depth on it here, but here is the reference you can look at. I only say that you can get assembly version there.
Then, in code, if you want to do things differently based on certain assembly version. If you want to compile based on version of framework or assembly - check this post
Bottom Line:
The fact that packages.config exist or not, or what it has in it, has no correlation to which DLL file has been used at compile time. VS has an incredible ability to crawl through directories and finding matching DLL files. packages.config can be outdated or desynchronized with the actual dll. This is not a bullet-proof way to do things.
packages.config
when you deploy your app? – Benco