The version of .NET against which you compile an application or assembly may not be the same version of .NET on which the application is currently running. A .NET application should always be able to run on the same or newer version of .NET against which it was compiled.

This is because .NET is backward compatible. This means that an application compiled on .NET v1.1 should run OK on .NET v2.0 and v3.0. But an application compiled on .NET v2.0 will not run on .NET v1.1.

  • To determine the .NET version on which an assembly was compiled, use the ImageRuntimeVersion property of the executing assembly.
  • To determine the .NET version on which an assembly is currently running, use the Environment.Version static property.

See the following code:

Assembly asm = Assembly.GetExecutingAssembly();
Console.WriteLine( "Compiled on .NET Version: {0}",
    asm.ImageRuntimeVersion.ToString() );
Console.WriteLine( "Running on .NET Version: {0}",
    Environment.Version.ToString() );

Note that the code above does not return the version of .NET against which it was compiled if you attempt to load the assembly through one of its types using the GetAssembly method as shown below. Instead it loads the version of .NET on which it is currently running, which is not the expected result. Getting the assembly with the GetExecutingAssembly method as shown above solves this problem. Please comment if you know why this is.

Assembly asm = Assembly.GetAssembly( typeof( Form1 ) );

Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot