Home >Backend Development >C++ >How Do I Efficiently Check the Installed .NET Framework Version on My System?
Easily determine .NET Framework version
Developers often need to determine the specific version of the .NET Framework used by a system, especially when targeting specific features or maintaining a specific version. Although Environment.Version()
is only available in the base version, finding an efficient way to check for specific updates such as .NET 3.5 SP1 becomes necessary.
Use the registry
One possible solution is to access the Windows Registry. For .NET versions 1-4, the following simple code can extract relevant information:
<code class="language-csharp">RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP"); string[] version_names = installed_versions.GetSubKeyNames(); //版本名称以 'v' 开头,例如 'v3.5',在转换之前需要将其去除 double Framework = Convert.ToDouble(version_names[version_names.Length - 1].Remove(0, 1), CultureInfo.InvariantCulture); int SP = Convert.ToInt32(installed_versions.OpenSubKey(version_names[version_names.Length - 1]).GetValue("SP", 0));</code>
Handling .NET 4.5
For .NET 4.5 and higher, Microsoft provides thorough documentation guidance. This code example demonstrates how to retrieve the exact version installed:
<code class="language-csharp">using System; using Microsoft.Win32; ... private static void Get45or451FromRegistry() { using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\")) { int releaseKey = Convert.ToInt32(ndpKey.GetValue("Release")); if (true) { Console.WriteLine("版本: " + CheckFor45DotVersion(releaseKey)); } } } ... // 使用 >= 检查版本将启用前向兼容性, // 但是您应该始终在新版本的框架上编译代码,以确保您的应用程序具有相同的运行效果。 private static string CheckFor45DotVersion(int releaseKey) { if (releaseKey >= 528040) { return "4.8 或更高版本"; } if (releaseKey >= 461808) { return "4.7.2 或更高版本"; } if (releaseKey >= 461308) { return "4.7.1 或更高版本"; } if (releaseKey >= 460798) { return "4.7 或更高版本"; } if (releaseKey >= 394802) { return "4.6.2 或更高版本"; } if (releaseKey >= 394254) { return "4.6.1 或更高版本"; } if (releaseKey >= 393295) { return "4.6 或更高版本"; } if (releaseKey >= 393273) { return "4.6 RC 或更高版本"; } if ((releaseKey >= 379893)) { return "4.5.2 或更高版本"; } if ((releaseKey >= 378675)) { return "4.5.1 或更高版本"; } if ((releaseKey >= 378389)) { return "4.5 或更高版本"; } // 此行永远不会执行。非空 release 密钥应表示 // 已安装 4.5 或更高版本。 return "未检测到 4.5 或更高版本"; }</code>
Using these methods, developers can now easily determine the exact version of .NET Framework installed on their systems, allowing them to confidently use specific features and resolve compatibility issues.
The above is the detailed content of How Do I Efficiently Check the Installed .NET Framework Version on My System?. For more information, please follow other related articles on the PHP Chinese website!