Get the list of .Net Frameworks that are installed on a Windows using C#

The answers are displayed on multiple websites. I have joined the code from these two links:

https://stackoverflow.com/questions/951856/is-there-an-easy-way-to-check-the-net-framework-version

https://www.c-sharpcorner.com/article/how-to-identify-which-net-framework-is-installed/

And got this solution:

public static List<string> GetList()
{
   List<string> res = new List<string>();
   RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP");
   string[] version_names = installed_versions.GetSubKeyNames();
   foreach(string version in version_names)
   {
      if (version == "CDF") continue;
      string tmp = "";
      //version names start with 'v', eg, 'v3.5' which needs to be trimmed off before conversion
      tmp += version.Replace("v", "");
      int SP = Convert.ToInt32(installed_versions.OpenSubKey(version).GetValue("SP", 0));
      if (SP > 0)
      {
         tmp += " SP" + SP;
      }
      res.Add(tmp);
   }
   using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"))
   {
      version_names = ndpKey.GetSubKeyNames();
      int releaseKey = Convert.ToInt32(ndpKey.GetValue("Release"));
      if (true)
      {
         res.Add(CheckFor45DotVersion(releaseKey));
      }
   }
   return res;
}
private static string CheckFor45DotVersion(int releaseKey)
{
   if (releaseKey >= 394802)
   {
      return "4.6.2 or later";
   }
   if (releaseKey >= 394254)
   {
      return "4.6.1 or later";
   }
   if (releaseKey >= 393273)
   {
      return "4.6 or later";
   }
   if ((releaseKey >= 379893))
   {
      return "4.5.2 or later";
   }
   if ((releaseKey >= 378675))
   {
      return "4.5.1 or later";
   }
   if ((releaseKey >= 378389))
   {
      return "4.5 or later";
   }
   // This line should never execute. A non-null release key should mean 
   // that 4.5 or later is installed. 
   return "No 4.5 or later version detected";
}

 

Leave a Reply