Close this search box.

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.

posted on Monday, January 30, 2006 2:09 PM

This article is part of the GWB Archives. Original Author: Lorin Thwaits

Related Posts