C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

There was a question that surfaced on the Arizona .NET User Group list about determining which versions of .NET are on a system, and which service packs have been applied.  It sounded like a fun challenge, so I spent a few minutes this afternoon learning about how to dig up that data.  Definitely the most helpful information came from this post in Aaron Stebner's blog, which was pointed out by my friend Tim Heuer.  Aaron's sample code is only available in native C++ and VB.NET, so I decided to write a simplified version in C#.  Here is the pertinent code to dig up that information from the registry, which will work for anything that has “.NET Framework“ in its name:

// (Note that lbInstVersions is a listbox placed on a WinForm or Web Form.)
string componentsKeyName="SOFTWARE\\Microsoft\\Active Setup\\Installed Components",
   friendlyName,
   version;
// Find out in the registry anything under:
//    HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components
// that has ".NET Framework" in the name
RegistryKey componentsKey=Registry.LocalMachine.OpenSubKey(componentsKeyName);
string[] instComps=componentsKey.GetSubKeyNames();
foreach(string instComp in instComps)
{
   RegistryKey key=componentsKey.OpenSubKey(instComp);
   friendlyName=(string)key.GetValue(null); // Gets the (Default) value from this key
   if(friendlyName != null && friendlyName.IndexOf(".NET Framework")>=0)
   {
      // Let's try to get any version information that's available
      version=(string)key.GetValue("Version");
      // If you want only the framework info with its SP level and not the
      // other hotfix and service pack detail, uncomment this if:
      //    if(version!=null && version.Split(',').Length>=4)
      lbInstVersions.Items.Add(friendlyName+(version!=null?(" ("+version+")"):""));
   }
}

On my system running in a WinForms app it brings up these results:

It should work on a Win32 or Win64 system to detect versions of the framework installed either as an OCX or from an MSI.  So far (and hopefully this will always be), only true versions of the .NET Framework show up with this filter.

The commented out IF statement in the code above (next to the last line) just tests how many pieces come back from the version number when you Split() on a comma.  If it's four or more, it assumes it's not a hotfix or service pack, and thus it's a true framework installation.  The fourth number in the version number is in fact always the current service pack level.

From here it would be easy to make a class to describe framework versions, with major and minor numbers.  Also from there an IComparer so it's sortable in an array or SortedList.  The sky's the limit.  But really something like this becomes useful before installation of a product, so a VBScript version would be handy.  Maybe I'll write one of those next if anyone would find it useful.

 


Feedback

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Lorin:

Thanks for this code. I found this while searching Google on how to determine the latest installed version of the .NET Framework. I added some custom code to compare the strings to determine which was greater. I wanted to share some code on how to determine the install directory of the latest version of the .NET Framework:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
class Program
{
[DllImport("mscoree.dll")]
private static extern int GetCORSystemDirectory(
[MarshalAs(UnmanagedType.LPWStr)]StringBuilder pbuffer,
int cchBuffer, ref int dwlength);

static void Main(string[] args)
{
GetClrInstallationDirectory();
}

private static void GetClrInstallationDirectory()
{
int MAX_PATH = 260;
StringBuilder sb = new StringBuilder(MAX_PATH);
GetCORSystemDirectory(sb, MAX_PATH, ref MAX_PATH);
Console.WriteLine(sb.ToString());
while(Console.Read() != 'q') ;
}

}
}



4/6/2006 4:42 AM | ike

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Of Course , you guy , two method done well .
And very useful.s 5/30/2006 9:07 PM | ms44cn

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Even i needed to do something similar..

Came across following property:

Environment.Version
It returns the latest version of the CLR installed.

Something as simple as that.
It also contains more properties to find further details like major, minor revisions etc

The Environment Class is pretty interesting and contains many other methods for finding other system details.

3/30/2007 12:05 PM | Sameer Palande

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Except that the .NET framework 3.0 does not show a version of 3, but a version of 2.

This is just to create a little confusion.

You can read more about this here:
http://blogs.msdn.com/brada/archive/2006/06/11/627128.aspx
5/31/2007 1:06 PM | Marco

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Hopefully this won't post a bunch of times - When I try to press the Post button - nothing seems to happen - so I've tried a few times now! ;)

This seems to work and all, as such... and thanks for the code - it's a start! :)

But I was looking for something that would list *all* the version*s* - as per the title: "C# code to determine which versions of the .NET Framework are installed..."

Either of the two example code segments tells me I have .NET 2.0 - but I really need something that will tell me I have .NET v1.0, v1.1, v2.0, and v3.0 32bit, and v2.0 and v3.0 64bit... in other words, code that will return a full list of the .NET versions installed.

Seen that anywhere? 8/6/2007 10:48 AM | unRheal

# re: C# code to determine which versions of the .NET Framework are installed on a system, along with hotfixes and Service Packs

Environment.Version as was correctly stated returns the CLR version. That is different to the .NET framework version.

Frameworks 1 and 1.1 came with CLR version 1. Frameworks 2 and upwards use CLR version 2.

So it's not wrong, just not useful if you are trying to determine the version of the framework installed! 3/5/2008 9:02 AM | andy

Post a comment





 

Please add 3 and 3 and type the answer here:

News


Welcome to my blog.
Here's what we've got on the menu today:

Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

Syndication: