13

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
justindao
  • 2,143
  • 3
  • 17
  • 32
  • 1
    Reading it as XML would probably make more sense. At which point in time do you need to know the package version? You could probably determine it from the compiled assembly version as well. – Matthew Jan 13 '16 at 19:48
  • Not sure exactly what you mean by "at which point in time", but I'd like to grab the version during runtime, to use elsewhere in the code. – justindao Jan 13 '16 at 21:43
  • Do you include `packages.config` when you deploy your app? – Aleksandr Ivanov Jan 13 '16 at 22:38
  • @AleksandrIvanov No we don't, so unfortunately this will not work. – justindao Jan 14 '16 at 00:49
  • Related: *[How can I get the NuGet package version programmatically from a NuGet feed?](https://stackoverflow.com/questions/26677801/how-can-i-get-the-nuget-package-version-programmatically-from-a-nuget-feed)* – Peter Mortensen Jun 25 '20 at 08:57

3 Answers3

6

If the package has any classes you can just get the assembly from that class and then the version. However, if the package has no classes, e.g. does something like copying files using nuget.targets then it's impossible this way.

Console.WriteLine("Using Selenium.WebDriver: " + Assembly.GetAssembly(typeof(OpenQA.Selenium.By)).GetName().Version.ToString());
lastlink
  • 1,026
  • 1
  • 13
  • 23
  • Note: this only gets the major version. E.g Serilog 2.4.0 -> 2.0.0.0 – Haukland Oct 14 '21 at 11:42
  • Even more general approach, just drop this anywhere in a method (also works with static methods): `string applicationVersion = Assembly.GetAssembly(MethodBase.GetCurrentMethod().DeclaringType).GetName().Version.ToString();` be sure to add at the top `using System.Reflection` namespace. – Jan Mar 16 '22 at 09:52
2

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
T.S.
  • 15,962
  • 10
  • 52
  • 71
  • 11
    The reason isn't important. This is a specific question that other people are searching for on the internet and a direct answer is the most helpful. – Warren Parks Aug 02 '18 at 22:24
  • 3
    @WarrenParks Reason (or, what in programming we call **motivation**) is super important; because most of the time there is an easy solution for a problem that is posted here. Only people go into wrong direction from the beginning. Let me tell you something about this question - the fact that packages.config exist or not, has no correlation to **which** dll has been used at compile time. packages.config can be outdated or desynchronized with actual dll. This is not bullet-proof way to do things and hence my answer. I am ok if you feel differently. Enjoy. – T.S. Aug 02 '18 at 23:27
  • 1
    **AGAIN**, there is no guarantee that assembly used at runtime is the same version as was used at compile time. This is why reflection needed to get assembly version. How you get it - is a different question. – T.S. Mar 04 '20 at 01:23
1

You can read the version from the assembly:

string assemblyVersion = Assembly.LoadFile('your assembly file').GetName().Version.ToString();
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Asad Mehmood
  • 125
  • 8