Click here to Skip to main content
Click here to Skip to main content

Get .NET Framework version

By , 19 Dec 2010
 
Check .NET Framework versions
 
This class has two functions. One gets you the latest version installed. The other one returns a boolean based on whether a specific version is installed or not.
 
public class NETVersionChecker
{
    public struct DOTNETVersionInfo
    {
        public double FrameworkVersion;
        public int ServicePack;
    }
    public static bool CheckRequiredDOTNETVersion(DOTNETVersionInfo required)
    {
        bool reslt = false;
        double tmpFramework = 0;
        int tmpSP = 0;
        try
        {
            RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP", false);
            string[] version_names = installed_versions.GetSubKeyNames();
            string tmpBaseVersion;
            //check each installed version
            foreach (string ver in version_names)
            {
                //set default values
                tmpFramework = 0;
                tmpSP = 0;
                tmpBaseVersion = string.Empty;
                try
                {
                    //version names start with 'v', eg, 'v3.5' which needs to be trimmed off before conversion
                    string tmpFullVersion = ver.Remove(0, 1);
                    //now remove the minor versions 2.0.5725
                    if (tmpFullVersion.Length > 3)
                    {
                        tmpBaseVersion = tmpFullVersion.Remove(tmpFullVersion.IndexOfAny((".").ToCharArray(), 2), tmpFullVersion.Length - 3);
                    }
                    else //its just 3 digit version
                    {
                        tmpBaseVersion = tmpFullVersion;
                    }
                    double basicVersion = 0;
                    if (double.TryParse(tmpBaseVersion, out basicVersion))
                    {
                        tmpFramework = basicVersion;
                    }
                }
                catch
                {
                    tmpFramework = 0;
                }
                //The service pack key might not exist so it might throw an error
                try
                {
                    tmpSP = Convert.ToInt32(installed_versions.OpenSubKey(ver).GetValue("SP", 0));
                }
                catch { }
                if (tmpFramework == required.FrameworkVersion && tmpSP == required.ServicePack)
                {
                    reslt = true;
                    break;
                }
            }
        }
        catch (Exception exp)
        {
            string message = "Error occured:" + exp.Message;
            if (exp is System.Security.SecurityException)
            {
                message += "\n Unable to find .NET Framework version. \n The user does not have the permissions required to access the registry key:\n"
                        + @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP";
            }
            MessageBox.Show(message);
        }
        return reslt;
    }
    public static DOTNETVersionInfo GetLatestDOTNETVersion()
    {
        DOTNETVersionInfo dnVer;
        dnVer.FrameworkVersion = 0;
        dnVer.ServicePack = 0;
        try
        {
            RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP", false);
            string[] version_names = installed_versions.GetSubKeyNames();
            //version names start with 'v', eg, 'v3.5' which needs to be trimmed off before conversion
            double Framework = Convert.ToDouble(version_names[version_names.Length - 1].Remove(0, 1), CultureInfo.InvariantCulture);
            dnVer.FrameworkVersion = Framework;
            //The service pack key might not exist so it might throw an error
            int SP = 0;
            try
            {
                SP = Convert.ToInt32(installed_versions.OpenSubKey(version_names[version_names.Length - 1]).GetValue("SP", 0));
            }
            catch { }
            dnVer.ServicePack = SP;
        }
        catch(Exception exp)
        {
            string message = "Error occured:" + exp.Message;
            if (exp is System.Security.SecurityException)
            {
                message += "\n Unable to find .NET Framework version. \n The user does not have the permissions required to access the registry key:\n"
                        + @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP";
            }
            MessageBox.Show(message);
        }
        return dnVer;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

cs981khx
Web Developer
United Kingdom United Kingdom
software developer, working in VB6 using com and active x controld with a legacy database.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralIt's always a good practice to add the required "using"s at ...memberben pour24-Jan-11 4:14 
GeneralReason for my vote of 5 Useful snippet!memberMember 280370921-Dec-10 22:39 
GeneralHey, I'm just wondering, what was the original usage scen...memberGotchaHunter15-Dec-10 9:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130619.1 | Last Updated 19 Dec 2010
Article Copyright 2010 by cs981khx
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid