Check if the specified .NET Framework Verson is Installed

The Environmet.Version property returns the version information of the CLR for the active .NET Framework. But what if we’d like to detect if a specific .NET Framework version is installed or not? Unfortunately we do not have a built in function or property that enables us to check for a specific .NET Framework version.

Here’s a simple solution which depends on a simple registry query.

/// <summary>
/// This routines detects if the specified .NET Framework version is installed or not
/// </summary>
/// <param name=”version”>Version Number</param>
/// <returns>A Boolean value that indicates if the specified .NET Framework version is installed or not</returns>
private Boolean IsFrameworkVersionInstalled(String version)
{
     Boolean result = false;
     RegistryKey componentsKey = Registry.LocalMachine.OpenSubKey(@”SOFTWARE\Microsoft\NET Framework Setup\NDP”);

     if (componentsKey != null)
     {
     string[] instComps = componentsKey.GetSubKeyNames();
     for (int i = 0; i < instComps.Length; i++)
     {
          String current = instComps[i];
          if (!String.IsNullOrEmpty(current))
          {
               if (current.StartsWith(String.Format(“v{0}”, version), StringComparison.OrdinalIgnoreCase))
               {
                    result = true;
                    break;
               }
          }
    }
}
return result;
}

The (@”SOFTWARE\Microsoft\NET Framework Setup\NDP” registry key contains the list of installed .NET Framework versions. The IsFrameworkVersionInstalled function queries this list fo the specified version number.

To check if the .NET Framework 3.0 installed or not, you can use the following line:
     IsFrameworkVersionInstalled(“3″);
To check if the .NET Framework 3.5 installed or not, you can use the following line:
     IsFrameworkVersionInstalled(“3.5″);

Of course, the following line will return false until you install .NET Framework 5.0. This means you need to wait till 2012 :)
     IsFrameworkVersionInstalled(“5.0″);

You can also many other methods that will help you to do same operation but i guess this is the one of the easiest soluions for this control.

Leave a Comment