using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.Win32; namespace Foundations { public static class DotNetVersionCheck { // ".NET Framework 4.5.1" public const int RequiredVersion = 378675; public static Dictionary ReleaseNumberToFramework = new Dictionary() { { 378389, @".NET Framework 4.5" }, { 378675, @".NET Framework 4.5.1" }, { 378758, @".NET Framework 4.5.1" }, { 379893, @".NET Framework 4.5.2" }, // On windows 10: { 393295, @".NET Framework 4.6" }, { 394254, @".NET Framework 4.6.1" }, { 394802, @".NET Framework 4.6.2" }, // On other OS versions: { 393297, @".NET Framework 4.6" }, { 394271, @".NET Framework 4.6.1" }, { 394806, @".NET Framework 4.6.2" }, }; public static bool CheckDotNetVersion() { // Look up the .NET version. See https://msdn.microsoft.com/en-us/library/hh925568#net_a bool pass = false; try { var regkey = "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"; using (RegistryKey ndpKey = RegistryKey .OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32) .OpenSubKey(regkey)) { int releaseKey = Convert.ToInt32(ndpKey.GetValue("Release")); // Must be at least 4.5.1 pass = releaseKey >= RequiredVersion; } } finally { if (!pass) { string versionString = ReleaseNumberToFramework[RequiredVersion]; MessageBox.Show(String.Format("You must install the {0} or later to continue.", versionString), "Exiting..."); } } return pass; } } }