Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created May 26, 2016 18:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save guitarrapc/d84776dda05ee99d3613ca9a7d7e38fc to your computer and use it in GitHub Desktop.
void Main()
{
Get45or451FromRegistry();
}
private static void Get45or451FromRegistry()
{
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"))
{
if (ndpKey != null && ndpKey.GetValue("Release") != null)
{
Console.WriteLine("Version: " + CheckFor45DotVersion((int)ndpKey.GetValue("Release")));
}
else
{
Console.WriteLine("Version 4.5 or later is not detected.");
}
}
}
// Checking the version using >= will enable forward compatibility,
// however you should always compile your code on newer versions of
// the framework to ensure your app works the same.
private static string CheckFor45DotVersion(int releaseKey)
{
if (releaseKey >= 393295)
{
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";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment