Tuesday, February 25, 2014

How to get installed .Net Framework version?

Here is the way to find out the installed version of .Net framework on your machine.

Manual Way
1.    Start run >> Regedit
2.  Go to “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\”
3.  Check keys under this. See image below for help.




C#.Net Code
Make sure to include following namespace in your class

       using Microsoft.Win32;


Method to get registry value

       public String GetVersion()
        {
            RegistryKey mobjSubRegistryKey;
            String strVersionDesc = "Installed: ";
            try
            {
                RegistryKey HKLM_Key = Registry.LocalMachine;
                mobjSubRegistryKey = HKLM_Key.OpenSubKey(
@"SOFTWARE\Microsoft\NET Framework Setup\NDP\");

                if (mobjSubRegistryKey == null)
                {
                    return "Not Installed";
                }

                String[] strarray = mobjSubRegistryKey.GetSubKeyNames();
               
                if (strarray == null)
                {
                    return "Not Installed";
                }

                foreach (String str in strarray)
                {
                    if (str == "v1.1.4322")
                    {
                        strVersionDesc += "[.Net Framework 1.1] ";
                    }
                    else if (str == "v2.0.50727")
                    {
                        strVersionDesc += "[.Net Framework 2.0] ";
                    }
                    else if (str == "v3.0")
                    {
                        strVersionDesc += "[.Net Framework 3.0] ";
                    }
                    else if (str == "v3.5")
                    {
                        strVersionDesc += "[.Net Framework 3.5] ";
                    }
                    else if (str == "v4")
                    {
                        strVersionDesc += "[.Net Framework 4.0] ";
                    }
                }

                return strVersionDesc;
            }
            catch(Exception ex)
            {
                return “An Error has occurred while capturing info”;
            }
        }

No comments:

Post a Comment