How to get assembly version without loading it
Hi all,
The other day I was trying to add a simple autoupdate functionality to a little tool I developed, and I needed to check the version of current assembly against the udpated one. If current assembly was older than the updated one, I needed to substitute the older one with the newer. Plain and simple.
This was my first attempt to achieve this (code has been simplified):
using System.Reflection;
using System.IO;
...
// Get current and updated assemblies
Assembly currentAssembly = Assembly.LoadFile(currentAssemblyPath);
Assembly updatedAssembly = Assembly.LoadFile(updatedAssemblyPath);
AssemblyName currentAssemblyName = currentAssembly.GetName();
AssemblyName updatedAssemblyName = updatedAssembly.GetName();
// Compare both versions
if (updatedAssemblyName.Version.CompareTo(currentAssemblyName.Version) <= 0)
{
// There's nothing to update
return;
}
// Update older version
File.Copy(updatedAssemblyPath, currentAssemblyPath, true);
But File.Copy failes because current assembly is in use. Why? Because of Assembly.LoadFile. When we load an assembly no other process (including ours) can change or delete the file because we are using it. The issue is that we can't unload an assembly that we loaded in an AppDomain unless the AppDomain itself gets unloaded. Here I'm using the default AppDomain which will only get unloaded when the application exits. So then I tried creating a new AppDomain, load the assemblies in there and unload the AppDomain afterwards before changing the file. It didn't help either. So...
How can we get the assembly version without loading the assembly? The solution is easy:
using System.Reflection;
using System.IO;
...
// Get current and updated assemblies
AssemblyName currentAssemblyName = AssemblyName.GetAssemblyName(currentAssemblyPath);
AssemblyName updatedAssemblyName = AssemblyName.GetAssemblyName(updatedAssemblyPath);
// Compare both versions
if (updatedAssemblyName.Version.CompareTo(currentAssemblyName.Version) <= 0)
{
// There's nothing to update
return;
}
// Update older version
File.Copy(updatedAssemblyPath, currentAssemblyPath, true);
AssemblyName.GetAssemblyName won't load the assembly, so we can change the file afterwards.
I hope this helps.
Kind regards,
Alex (Alejandro Campos Magencio)
PS: In case you are interested, I will post my simple autoupdate code one of these days, once I have time to change a couple of things on it.
Comments
Anonymous
September 11, 2008
Great solution! I've been searching for that a long time ago!Thank you!Anonymous
September 12, 2008
Great tip. Just what I was looking for. Thanks!Anonymous
September 24, 2008
Faced the same issues, and considered going down the Appdomain path , till I saw your post. Good one.Anonymous
October 16, 2008
It's interesting reading this article and comparing .NET with Java. I had to do this in Java some months ago. There were several problems:JAR Version is an OPTIONAL value stored in the JAR's manifest file. So you can't rely in having a version value in every JAR file.You can write the on-disk JAR file on the fly without problems. You only need to reload the JAR file afterwards to get the updated one to memory. So, in this little comparison, one up, one down. Great article!Anonymous
January 11, 2009
Dang, that was easy!First I have used Assembly.ReflectionOnlyLoad with CustomAttributeData class and that actually worked, but I had to apply the same fix to our older solution, which is compiled with .NET 1.1 and those methods were not introduced yet. Luckily, quick search brought me to your solution, which is much easier and cleaner.Thanks!Anonymous
February 27, 2009
Doesn't seem to work on Compact Framework 2.0 though.AssemblyName.GetAssemblyName is not supported.:-(Anonymous
September 06, 2009
The comment has been removedAnonymous
February 03, 2010
Thanks for this code. I've been trying to find a way to do this without actually loading the file.Anonymous
March 22, 2011
Thx for teh code! Nice sollution.Anonymous
September 08, 2011
"Duh", I said to myself after reading. Thanks!Anonymous
September 10, 2012
THANK YOU **Anonymous
September 27, 2015
Bit of a problem m8, I have this third party assembly and I used this method to interrogate it's version. I get 1.0.5023.14735 but wen I open the same file in the the windows explorer and click on Properties->Details I get 2.10.8.2935. Can anyone say why there's a difference?