gpt4 book ai didi

c# - 有没有一种简单的方法来检查 .NET Framework 版本?

转载 作者:IT王子 更新时间:2023-10-29 03:40:18 25 4
gpt4 key购买 nike

问题是我需要知道它是否是版本 3.5 SP 1。Environment.Version() 只返回 2.0.50727.3053

我找到了 this solution ,但我认为这将花费比其值(value)更多的时间,所以我正在寻找一个更简单的方法。可能吗?

最佳答案

像这样的东西应该可以做到。只需从注册表中获取值

对于 .NET 1-4:

Framework 是安装的最高版本,SP 是该版本的服务包。

RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP");
string[] version_names = installed_versions.GetSubKeyNames();
//version names start with 'v', eg, 'v3.5' which needs to be trimmed off before conversion
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));

对于 .NET 4.5+(来自 official documentation):

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("Version: " + CheckFor45DotVersion(releaseKey));
}
}
}


...


// 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 >= 528040) {
return "4.8 or later";
}
if (releaseKey >= 461808) {
return "4.7.2 or later";
}
if (releaseKey >= 461308) {
return "4.7.1 or later";
}
if (releaseKey >= 460798) {
return "4.7 or later";
}
if (releaseKey >= 394802) {
return "4.6.2 or later";
}
if (releaseKey >= 394254) {
return "4.6.1 or later";
}
if (releaseKey >= 393295) {
return "4.6 or later";
}
if (releaseKey >= 393273) {
return "4.6 RC 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";
}

关于c# - 有没有一种简单的方法来检查 .NET Framework 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/951856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com