Listing Installed Versions of .Net Framework in C#

The internets know many answers to the question how to get a list of all installed versions of the .Net Framework. (Hint: registry path HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP)

Looking for some kind of “official” confirmation, I finally found this article in the MS Knowledge Base, How to determine which versions and service pack levels of the Microsoft .NET Framework are installed.

Converting this information into code

  • Open the registry key and loop through its subkeys
static void Main(string[] args)
{
  Console.WriteLine(".Net Framework Versions");

  var hkNDP = Registry.LocalMachine.OpenSubKey(
    @"SOFTWARE\Microsoft\NET Framework Setup\NDP", false);
  WriteKey(hkNDP, "");
  hkNDP.Close();

  Console.ReadLine();
}
  • For the subkeys, detect Version, SP, and Install values, and loop through subkeys
static void WriteKey(RegistryKey hk, string relPath)
{
  if (relPath != "")
    relPath += "/";

  foreach (var keyname in hk.GetSubKeyNames())
  {
    var key = hk.OpenSubKey(keyname, false);

    var keySP = key.GetValue("SP");
    var keyVersion = key.GetValue("Version");

    if (keyVersion != null)
      Console.WriteLine(relPath + keyname +
        ": Version " + keyVersion.ToString() +
        ((keySP != null) ? " SP " + keySP.ToString() : "") +
        (1.Equals(key.GetValue("Install")) ? " installed" : ""));

    WriteKey(key, relPath + keyname);

    key.Close();
  }
}

On Windows 7 (German), this results in the following output

.Net Framework Versions
v2.0.50727: Version 2.0.50727.4927 SP 2 installed
v2.0.50727/1031: Version 2.0.50727.4927 SP 2 installed
v3.0: Version 3.0.30729.4926 SP 2 installed
v3.0/Setup: Version 3.0.30729.4926
v3.0/Setup/1031: Version 3.0.30729.4926 SP 2 installed
v3.0/Setup/Windows Communication Foundation: Version 3.0.4506.4926
v3.0/Setup/Windows Presentation Foundation: Version 3.0.6920.4902
v3.5: Version 3.5.30729.4926 SP 1 installed
v3.5/1031: Version 3.5.30729.4926 SP 1 installed
v4/Client: Version 4.0.30319 installed
v4/Client/1033: Version 4.0.30319 installed
v4/Full: Version 4.0.30319 installed
v4/Full/1033: Version 4.0.30319 installed
Advertisements

One Response to Listing Installed Versions of .Net Framework in C#

  1. […] The command netver lists all installed versions of the .Net framework. […]

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

%d bloggers like this: