Created
November 30, 2017 16:44
-
-
Save kristianjaeger/3deb8a9d1b56888ce2e7553ff7709162 to your computer and use it in GitHub Desktop.
A utility to check the .NET Framework version instead of manually checking the registry and comparing the key value (use .NET 4 target framework)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: this is a slightly modified version of an example from - https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed | |
using System; | |
using Microsoft.Win32; | |
namespace GetDotNetVersion | |
{ | |
public class GetDotNetVersion | |
{ | |
public static void Main() | |
{ | |
Get45PlusFromRegistry(); | |
} | |
private static void Get45PlusFromRegistry() | |
{ | |
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"; | |
using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey)) | |
{ | |
if (ndpKey?.GetValue("Release") != null) | |
{ | |
Console.WriteLine(".NET Framework Version: " + CheckFor45PlusVersion((int)ndpKey.GetValue("Release"))); | |
} | |
else | |
{ | |
Console.WriteLine(".NET Framework Version 4.5 or later is not detected."); | |
} | |
} | |
} | |
// Checking the version using >= will enable forward compatibility. | |
private static string CheckFor45PlusVersion(int releaseKey) | |
{ | |
if (releaseKey >= 460798) | |
{ | |
return "4.7 or later"; | |
} | |
if (releaseKey >= 394802) | |
{ | |
return "4.6.2"; | |
} | |
if (releaseKey >= 394254) | |
{ | |
return "4.6.1"; | |
} | |
if (releaseKey >= 393295) | |
{ | |
return "4.6"; | |
} | |
if (releaseKey >= 379893) | |
{ | |
return "4.5.2"; | |
} | |
if (releaseKey >= 378675) | |
{ | |
return "4.5.1"; | |
} | |
if (releaseKey >= 378389) | |
{ | |
return "4.5"; | |
} | |
// This code 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"; | |
} | |
} | |
} | |
// This example displays output like the following: | |
// .NET Framework Version: 4.6.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment