Detecting a .NET assembly version

Posted: (EET/GMT+2)

 

Sometimes, you need to know the version of a given .NET assembly. For instance, if your application loads a assembly complied into DLL, you might wish to check its version before starting to use it.

Now, to set the version of an assembly, you would use the System.Reflection.AssemblyVersionAttribute class like this:

[assembly: AssemblyVersion("0.9.*")]

Then, in the assembly itself (or another, just as you choose) you can use the following code to return a version string:

public static string GetEngineVersion() 
{
  System.Reflection.Assembly thisDLL =
    Assembly.GetExecutingAssembly();
  return thisDLL.GetName().Version.ToString();
}

So, the key is to get the currently executing assembly using the System.Reflection.Assembly class, and then use the GetName() method to get the System.Reflection.AssemblyName class. This class has a Version property, which is all you need.