检测是否在OS中安装了.net framework 4.5.X?

时间:2015-04-11 02:35:25

标签: c# windows .net-framework-version

如果当前操作系统上安装了.net framework 4.5.X,我需要检测什么?

我尝试this但似乎仅限于使用非官方更新的框架4.0。

3 个答案:

答案 0 :(得分:3)

刚刚为您进行了Google搜索,并发现以下MSDN文章直接引用了您的问题。包括4.5及更高版本的示例...

https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx

答案 1 :(得分:2)

您可以查看注册表或只是签入已安装的程序。 在“开始”菜单上,键入" appwiz.cpl",然后搜索.net 您将在计算机上看到所有.net。

答案 2 :(得分:0)

// 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";
}

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.");
  } 
}

}