Yesterday I just quickly wanted to add the version number of our new Todo management application to the footer of every page. I could have just checked our own blog history or went off to Google or Stackoverflow but I just wanted to do it myself. Simple enough, isn’t it?
To get the assembly of the web application from inside a view, for example, unfortunately this won’t work:
It will print out something like Version: App_Web_xu4dep5e, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Well, this is the dynamically compiled dll for just my view – not what I’m looking for.
The following does the trick:
This prints Version: Todo.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
So now, the only thing left is to get the version number from the assembly. Should be easy, there should be some kind of property named Version, or a method called GetVersion() right? Well, there isn’t:
Looking at the AssemblyInfo.cs, I saw that the AssemblyVersion is an attribute on the assembly:
So I thought the following code would work, but it didn’t (throws on .First() with System.InvalidOperationException: Sequence contains no elements):
In the end, the solution is really short but not quite intuitive. Why is Version a member of the type AssemblyName which you access by calling GetName()? I don’t know.
Happy Coding!