Determine .NET versions installed on a machine

Hello World,

I’m sure most of us have had an encounter with a machine that needs to run our .NET app. Now, if you had to quickly check if the machine had .NET installed on it (Windows 7+ would have .NET installed as part of the OS I believe) or if it had the version your app requires it can be a tricky task at first look. There are myriad ways you can do this though, both manually and programmatically. This post intends to list out a few of those ways that shall come in handy.

The CLRVER tool

On your own machine, if you have the .NET SDK installed, you can quickly launch the developer command prompt available under the Visual studio node in your menu. The following command would then list out all .NET framework versions installed on your machine

clrver

Good news is, you can simply copy the CLRVER.exe to another machine and use the same command to find information on installed .NET versions. So you can package the EXE with a DOS command that can execute the command on any windows machine!

You can even use the -all command line option to enlist all processes using the CLR and the version of .NET they are using. For a specific process id, the tool can find out the version of .NET in use by using the command (assuming the PID of interest is 1234):

clrver 1234

More information on the CLRVER tool is available here.

Programmatically using C#

This turns out to be very simple, albeit with the caveat that since you’d need .NET framework to run this, it may not be very useful if the target machine does not have the required version of .NET for your app. Anyways, it takes only one line of code:

using System;

public class Program
{
   public static void Main()
   {
     Console.WriteLine("Version: {0}", Environment.Version.ToString()); // CAUTION: Only prints the version used by you app
   }

Using PowerShell built-in cmdlets

Most (if not all) Windows machines are going to have PowerShell on them, so it becomes really trivial to find out the .NET version installed on a machine. Note the semi-colon after the registry hive name.

Get-ChildItem "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP"

You would get something like below as a result of running this cmdlet:

Name         Property
----         --------
CDF
v2.0.50727   Install : 1
             Version : 2.0.50727.5420
             Increment : 5420
             SP : 2
             CBS : 1
             OCM : 1
v3.0         Version : 3.0.30729.5420
             CBS : 1
             Increment : 5420
             Install : 1 
             SP : 2
v3.5         Version : 3.5.30729.5420
             CBS : 1 
             Install : 1
InstallPath :
             C:\Windows\Microsoft.NET\Framework64\v3.5\
            SP : 1
v4
            v4.0 (default) : deprecated

 Finding out the version of .NET a 3rd Party assembly requires

This can be done as follows (the list is not exhaustive, but covers the most common ways)

1. Use a disassembler e.g. ILDASM, dotPeek, Reflector look a the reference to mscorlib

ILDASM would give an output similar to the following

// Metadata version: v2.0.50727
.assembly extern mscorlib
{
  .publickeytoken =(B7 7A5C561934 E0 89)// .z\V.4...ver 2:0:0:0
}

2. (Cute!) Refer it in a VS solution and look at properties (F4 or right click the reference->Properties)

3. Using C# code: [Note that for non .NET DLLs, this will throw an exception]

using System.Reflection;
using System.Runtime.Versioning;
.
.
var tar = (TargetFrameworkAttribute) Assembly.LoadFrom(@"D:\Something.dll").GetCustomAttributes(typeof(TargetFrameworkAttribute)).First();

4. Using corflags: [be careful not to use any command line switches lest you’d modify the PE header of the assembly]

C:\Program Files (x86)\Microsoft Visual Studio 11.0>corflags "D:\Bin\MyAssembly.dll"

corflags would print something similar to below:

Version    : v4.0.30319
CLR Header : 2.5
PE         : PE32
CorFlags   : 0x1
ILONLY     : 1
32BITREQ   : 0
32BITPREF  : 0
Signed     : 0

 

Phew! So that’s pretty much it! Have fun!

Happy Coding.

Advertisements
Tagged with: ,
Posted in .NET, Reflection

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

  • Comic for July 25, 2017
    Dilbert readers - Please visit Dilbert.com to read this feature. Due to changes with our feeds, we are now making this RSS feed a link to Dilbert.com.
%d bloggers like this: