如何:确定已安装的 .NET Framework 版本How to: Determine which .NET Framework versions are installed

用户可在他们的计算机上安装和运行 .NET Framework 的多个版本。Users can install and run multiple versions of .NET Framework on their computers. 当你开发或部署应用时,你可能需要知道用户的计算机上安装了哪些 .NET Framework 版本。When you develop or deploy your app, you might need to know which .NET Framework versions are installed on the user's computer. 注册表包含计算机上安装的 .NET Framework 版本列表。The registry contains a list of the versions of .NET Framework installed on the computer.

.NET Framework 由两个采用不同版本的主要组件构成:.NET Framework consists of two main components, which are versioned separately:

  • 一组程序集,它们是为应用提供功能的类型与资源的集合。A set of assemblies, which are collections of types and resources that provide the functionality for your apps. .NET Framework 和程序集使用相同的版本号。.NET Framework and the assemblies share the same version number. 例如,.NET Framework 版本包括 4.5、4.6.1 和 4.7.2。For example, .NET Framework versions include 4.5, 4.6.1, and 4.7.2.

  • 公共语言运行时 (CLR),可管理并执行应用代码。The common language runtime (CLR), which manages and executes your app's code. 单个 CLR 版本通常可支持多个 .NET Framework 版本。A single CLR version typically supports multiple .NET Framework versions. 例如,CLR 版本4.0.30319.xxxxx(其中 xxxxx 小于42000)支持 .NET Framework 版本 4 到 4.5.2。 For example, CLR version 4.0.30319.xxxxx where xxxxx is less than 42000, supports .NET Framework versions 4 through 4.5.2. 大于或等于4.0.30319.42000 的 CLR 版本支持从 .NET Framework 4.6 开始的 .NET Framework 版本。CLR version greater than or equal to 4.0.30319.42000 supports .NET Framework versions starting with .NET Framework 4.6.

可使用社区维护的工具帮助检测安装了哪些 .NET Framework 版本:Community-maintained tools are available to help detect which .NET Framework versions are installed:

要了解如何检测已安装的每个 .NET Framework 版本的更新,请参阅操作说明:确定已安装的 .NET Framework 更新For information about detecting the installed updates for each version of .NET Framework, see How to: Determine which .NET Framework updates are installed.

检测 .NET Framework 4.5 及更高版本Detect .NET Framework 4.5 and later versions

计算机上安装的 .NET Framework 版本(4.5 及更高版本)列出在注册表中,位于 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full。The version of .NET Framework (4.5 and later) installed on a machine is listed in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full. 如果缺少 Full 子项,则未安装 .NET Framework 4.5 或更高版本。If the Full subkey is missing, then .NET Framework 4.5 or above isn't installed.

备注

注册表路径中的 .NET Framework Setup 子项不以句点开头。The NET Framework Setup subkey in the registry path does not begin with a period.

注册表中的 Release REG_DWORD 值代表已安装的 .NET Framework 版本。The Release REG_DWORD value in the registry represents the version of .NET Framework installed.

.NET Framework 版本.NET Framework version Release 的值Value of Release
.NET Framework 4.5.NET Framework 4.5 所有 Windows 操作系统:378389All Windows operating systems: 378389
.NET Framework 4.5.1.NET Framework 4.5.1 在 Windows 8.1 和 Windows Server 2012 R2 上:378675On Windows 8.1 and Windows Server 2012 R2: 378675
在所有其他 Windows 操作系统上:378758On all other Windows operating systems: 378758
.NET Framework 4.5.2.NET Framework 4.5.2 所有 Windows 操作系统:379893All Windows operating systems: 379893
.NET Framework 4.6.NET Framework 4.6 在 Windows 10 上:393295On Windows 10: 393295
在所有其他 Windows 操作系统上:393297On all other Windows operating systems: 393297
.NET Framework 4.6.1.NET Framework 4.6.1 在 Windows 10 11 月更新系统上:394254On Windows 10 November Update systems: 394254
在所有其他 Windows 操作系统(包括 Windows 10)上:394271On all other Windows operating systems (including Windows 10): 394271
.NET Framework 4.6.2.NET Framework 4.6.2 在 Windows 10 周年更新和 Windows Server 2016 上:394802On Windows 10 Anniversary Update and Windows Server 2016: 394802
在所有其他 Windows 操作系统(包括其他 Windows 10 操作系统)上:394806On all other Windows operating systems (including other Windows 10 operating systems): 394806
.NET Framework 4.7.NET Framework 4.7 在 Windows 10 创意者更新上:460798On Windows 10 Creators Update: 460798
在所有其他 Windows 操作系统(包括其他 Windows 10 操作系统)上:460805On all other Windows operating systems (including other Windows 10 operating systems): 460805
.NET Framework 4.7.1.NET Framework 4.7.1 在 Windows 10 Fall Creators Update 和 Windows Server 版本 1709 上:461308On Windows 10 Fall Creators Update and Windows Server, version 1709: 461308
在所有其他 Windows 操作系统(包括其他 Windows 10 操作系统)上:461310On all other Windows operating systems (including other Windows 10 operating systems): 461310
.NET Framework 4.7.2.NET Framework 4.7.2 在 Windows 10 2018 年 4 月更新和 Windows Server 版本 1803 上:461808On Windows 10 April 2018 Update and Windows Server, version 1803: 461808
在除 Windows 10 2018 年 4 月更新和 Windows Server 版本 1803 之外的所有 Windows 操作系统上:461814On all Windows operating systems other than Windows 10 April 2018 Update and Windows Server, version 1803: 461814
.NET Framework 4.8.NET Framework 4.8 在 Windows 10 2019 年 5 月更新和 Windows 10 2019 年 11 月更新上:528040On Windows 10 May 2019 Update and Windows 10 November 2019 Update: 528040
在 Windows 10 2020 年 5 月更新上:528372On Windows 10 May 2020 Update: 528372
在所有其他 Windows 操作系统(包括其他 Windows 10 操作系统)上:528049On all other Windows operating systems (including other Windows 10 operating systems): 528049

最低版本Minimum version

若要确定是否安装了最低版本的 .NET Framework,请使用上表中最小的 Release REG_DWORD 值来指示该版本。To determine whether a minimum version of .NET Framework is present, use the smallest Release REG_DWORD value for that version from the previous table.

例如,如果应用程序在 .NET Framework 4.8 或更高版本下运行,请测试 Release REG_DWORD 值是否大于或等于 528040。For example, if your application runs under .NET Framework 4.8 or a later version, test for a Release REG_DWORD value that is greater than or equal to 528040.

.NET Framework 版本.NET Framework version 最小值Minimum value
.NET Framework 4.5.NET Framework 4.5 378389378389
.NET Framework 4.5.1.NET Framework 4.5.1 378675378675
.NET Framework 4.5.2.NET Framework 4.5.2 379893379893
.NET Framework 4.6.NET Framework 4.6 393295393295
.NET Framework 4.6.1.NET Framework 4.6.1 394254394254
.NET Framework 4.6.2.NET Framework 4.6.2 394802394802
.NET Framework 4.7.NET Framework 4.7 460798460798
.NET Framework 4.7.1.NET Framework 4.7.1 461308461308
.NET Framework 4.7.2.NET Framework 4.7.2 461808461808
.NET Framework 4.8.NET Framework 4.8 528040528040

使用注册表编辑器Use Registry Editor

  1. 在“开始”菜单中,选择“运行”,输入“regedit”,然后选择“确定” 。From the Start menu, choose Run, enter regedit, and then select OK.

(必须具有管理凭据才能运行 regedit。)(You must have administrative credentials to run regedit.)

  1. 在注册表编辑器中,打开以下子项:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full。In the Registry Editor, open the following subkey: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full. 如果“Full”子项不存在,则表示尚未安装 .NET Framework 4.5 或更高版本。If the Full subkey isn't present, then you don't have .NET Framework 4.5 or later installed.

  2. 请检查名为“Release”的 REG_DWORD 条目。Check for a REG_DWORD entry named Release. 如果存在,则已安装 .NET Framework 4.5 或更高版本。If it exists, then you have .NET Framework 4.5 or later installed. 其值对应于 .NET Framework 的特定版本。Its value corresponds to a particular version of .NET Framework. 以下图为例,“Release”条目的值为 528040,这是 .NET Framework 4.8 的版本密钥。In the following figure, for example, the value of the Release entry is 528040, which is the release key for .NET Framework 4.8.

.NET Framework 4.5 的注册表项

使用 PowerShell 检查最低版本Use PowerShell to check for a minimum version

使用 PowerShell 命令检查 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full 子项“Release”条目的值\。Use PowerShell commands to check the value of the Release entry of the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full subkey.

以下示例检查“Release”条目的值,以确定是否已安装 .NET Framework 4.6.2 或更高版本。The following examples check the value of the Release entry to determine whether .NET Framework 4.6.2 or later is installed. 如果安装了此代码,则返回 True,否则返回 FalseThis code returns True if it's installed and False otherwise.

(Get-ItemProperty "HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full").Release -ge 394802

使用代码查询注册表Query the registry using code

  1. 使用 RegistryKey.OpenBaseKeyRegistryKey.OpenSubKey 方法访问 Windows 注册表中的 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full 子项。Use the RegistryKey.OpenBaseKey and RegistryKey.OpenSubKey methods to access the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full subkey in the Windows registry.

    重要

    如果运行的应用是 32 位且在 64 位 Windows 中运行,则注册表路径与前面列出的不同。If the app you're running is 32-bit and running in 64-bit Windows, the registry paths will be different than previously listed. 可在 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ 子项中找到 64 位注册表。The 64-bit registry is available in the HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ subkey. 例如,.NET Framework 4.5 的注册表子项为 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v4\Full。For example, the registry subkey for .NET Framework 4.5 is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v4\Full.

  2. 检查 Release REG_DWORD 值以确定已安装的版本。Check the Release REG_DWORD value to determine the installed version. 为了向前兼容,可检查是否有一个值大于或等于 .NET Framework 版本表中所列的值。To be forward-compatible, check for a value greater than or equal to the value listed in the .NET Framework version table.

以下示例检查注册表中“Release”条目的值,以查找已安装的 .NET Framework 4.5-4.8:The following example checks the value of the Release entry in the registry to find the versions of .NET Framework 4.5-4.8 that are installed:

const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";

using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
    if (ndpKey != null && 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 >= enables forward compatibility.
static string CheckFor45PlusVersion(int releaseKey)
{
    if (releaseKey >= 528040)
        return "4.8 or later";
    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 >= 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";
}
Private Sub Get45PlusFromRegistry()
    Const subkey As String = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"

    Using ndpKey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey)
        If ndpKey IsNot Nothing AndAlso ndpKey.GetValue("Release") IsNot Nothing Then
            Console.WriteLine($".NET Framework Version: {CheckFor45PlusVersion(ndpKey.GetValue("Release"))}")
        Else
            Console.WriteLine(".NET Framework Version 4.5 or later is not detected.")
        End If
    End Using
End Sub

' Checking the version using >= enables forward compatibility.
Private Function CheckFor45PlusVersion(releaseKey As Integer) As String
    If releaseKey >= 528040 Then
        Return "4.8 or later"
    ElseIf releaseKey >= 461808 Then
        Return "4.7.2"
    ElseIf releaseKey >= 461308 Then
        Return "4.7.1"
    ElseIf releaseKey >= 460798 Then
        Return "4.7"
    ElseIf releaseKey >= 394802 Then
        Return "4.6.2"
    ElseIf releaseKey >= 394254 Then
        Return "4.6.1"
    ElseIf releaseKey >= 393295 Then
        Return "4.6"
    ElseIf releaseKey >= 379893 Then
        Return "4.5.2"
    ElseIf releaseKey >= 378675 Then
        Return "4.5.1"
    ElseIf releaseKey >= 378389 Then
        Return "4.5"
    End If
    ' 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"
End Function

此示例显示类似以下内容的输出:The example displays output like the following:

.NET Framework Version: 4.6.1

此示例遵循版本检查的建议做法:This example follows the recommended practice for version checking:

  • 检查“Release”条目的值是否大于或等于已知版本密钥的值。It checks whether the value of the Release entry is greater than or equal to the value of the known release keys.
  • 按从最新版本到最早版本的顺序检查。It checks in order from most recent version to earliest version.

检测 .NET Framework 1.0 到 4.0Detect .NET Framework 1.0 through 4.0

.NET Framework 1.1 到 4.0 的每个版本都作为子项列出在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP。Each version of .NET Framework from 1.1 to 4.0 is listed as a subkey at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP. 下表列出了每个 .NET Framework 版本的路径。The following table lists the path to each .NET Framework version. 对于大多数版本,都有 Install REG_DWORD 值 1,指示已安装此版本。For most versions, there's an Install REG_DWORD value of 1 to indicate this version is installed. 在这些子项中,还有一个包含版本字符串的 Version REG_SZ 值。In these subkeys, there's also a Version REG_SZ value that contains a version string.

备注

注册表路径中的 .NET Framework Setup 子项不以句点开头。The NET Framework Setup subkey in the registry path does not begin with a period.

Framework 版本Framework Version 注册表子项Registry Subkey “值”Value
1.01.0 HKLM\Software\Microsoft\.NETFramework\Policy\v1.0\3705HKLM\Software\Microsoft\.NETFramework\Policy\v1.0\3705 Install REG_SZ 等于 1Install REG_SZ equals 1
1.11.1 HKLM\Software\Microsoft\NET Framework Setup\NDP\v1.1.4322HKLM\Software\Microsoft\NET Framework Setup\NDP\v1.1.4322 Install REG_DWORD 等于 1Install REG_DWORD equals 1
2.02.0 HKLM\Software\Microsoft\NET Framework Setup\NDP\v2.0.50727HKLM\Software\Microsoft\NET Framework Setup\NDP\v2.0.50727 Install REG_DWORD 等于 1Install REG_DWORD equals 1
3.03.0 HKLM\Software\Microsoft\NET Framework Setup\NDP\v3.0\SetupHKLM\Software\Microsoft\NET Framework Setup\NDP\v3.0\Setup InstallSuccess REG_DWORD 等于 1InstallSuccess REG_DWORD equals 1
3.53.5 HKLM\Software\Microsoft\NET Framework Setup\NDP\v3.5HKLM\Software\Microsoft\NET Framework Setup\NDP\v3.5 Install REG_DWORD 等于 1Install REG_DWORD equals 1
4.0 客户端配置文件4.0 Client Profile HKLM\Software\Microsoft\NET Framework Setup\NDP\v4\ClientHKLM\Software\Microsoft\NET Framework Setup\NDP\v4\Client Install REG_DWORD 等于 1Install REG_DWORD equals 1
4.0 完整配置文件4.0 Full Profile HKLM\Software\Microsoft\NET Framework Setup\NDP\v4\FullHKLM\Software\Microsoft\NET Framework Setup\NDP\v4\Full Install REG_DWORD 等于 1Install REG_DWORD equals 1

重要

如果运行的应用是 32 位且在 64 位 Windows 中运行,则注册表路径与前面列出的不同。If the app you're running is 32-bit and running in 64-bit Windows, the registry paths will be different than previously listed. 可在 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ 子项中找到 64 位注册表。The 64-bit registry is available in the HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ subkey. 例如,.NET Framework 3.5 的注册表子项为 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v3.5。For example, the registry subkey for .NET Framework 3.5 is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v3.5.

请注意,.NET Framework 1.0 子项的注册表路径与其他子项不同。Notice that the registry path to the .NET Framework 1.0 subkey is different from the others.

使用注册表编辑器(较旧的框架版本)Use Registry Editor (older framework versions)

  1. 在“开始”菜单中,选择“运行”,输入“regedit”,然后选择“确定” 。From the Start menu, choose Run, enter regedit, and then select OK.

    必须具有管理凭据才能运行 regedit。You must have administrative credentials to run regedit.

  2. 打开与要检查的版本匹配的子项。Open the subkey that matches the version you want to check. 使用检测 .NET Framework 1.0 到 4.0部分列出的表。Use the table in the Detect .NET Framework 1.0 through 4.0 section.

    下图显示了 .NET Framework 3.5 的子项及其 Version 值。The following figure shows the subkey and its Version value for .NET Framework 3.5.

    .NET Framework 3.5 的注册表项。The registry entry for .NET Framework 3.5.

使用代码查询注册表(较旧的框架版本)Query the registry using code (older framework versions)

使用 Microsoft.Win32.RegistryKey 类访问 Windows 注册表中的 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP 子项。Use the Microsoft.Win32.RegistryKey class to access the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP subkey in the Windows registry.

重要

如果运行的应用是 32 位且在 64 位 Windows 中运行,则注册表路径与前面列出的不同。If the app you're running is 32-bit and running in 64-bit Windows, the registry paths will be different than previously listed. 可在 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ 子项中找到 64 位注册表。The 64-bit registry is available in the HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ subkey. 例如,.NET Framework 3.5 的注册表子项为 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v3.5。For example, the registry subkey for .NET Framework 3.5 is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v3.5.

以下示例查找已安装的 .NET Framework 1-4:The following example finds the versions of .NET Framework 1-4 that are installed:

// Open the registry key for the .NET Framework entry.
using RegistryKey ndpKey =
        RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).
        OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\");

foreach (var versionKeyName in ndpKey.GetSubKeyNames())
{
    // Skip .NET Framework 4.5 version information.
    if (versionKeyName == "v4")
    {
        continue;
    }

    if (versionKeyName.StartsWith("v"))
    {
        RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);

        // Get the .NET Framework version value.
        var name = (string)versionKey.GetValue("Version", "");
        // Get the service pack (SP) number.
        var sp = versionKey.GetValue("SP", "").ToString();

        // Get the installation flag.
        var install = versionKey.GetValue("Install", "").ToString();
        if (string.IsNullOrEmpty(install))
        {
            // No install info; it must be in a child subkey.
            Console.WriteLine($"{versionKeyName}  {name}");
        }
        else if (install == "1")
        {
            // Install = 1 means the version is installed.

            if (!string.IsNullOrEmpty(sp))
            {
                Console.WriteLine($"{versionKeyName}  {name}  SP{sp}");
            }
            else
            {
                Console.WriteLine($"{versionKeyName}  {name}");
            }
        }

        if (!string.IsNullOrEmpty(name))
        {
            continue;
        }
        // else print out info from subkeys...

        // Iterate through the subkeys of the version subkey.
        foreach (var subKeyName in versionKey.GetSubKeyNames())
        {
            RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
            name = (string)subKey.GetValue("Version", "");
            if (!string.IsNullOrEmpty(name))
                sp = subKey.GetValue("SP", "").ToString();

            install = subKey.GetValue("Install", "").ToString();
            if (string.IsNullOrEmpty(install))
            {
                // No install info; it must be later.
                Console.WriteLine($"{versionKeyName}  {name}");
            }
            else if (install == "1")
            {
                if (!string.IsNullOrEmpty(sp))
                {
                    Console.WriteLine($"{subKeyName}  {name}  SP{sp}");
                }
                else
                {
                    Console.WriteLine($"  {subKeyName}  {name}");
                }
            }
        }
    }
}
' Opens the registry key for the .NET Framework entry.
Using ndpKey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).
    OpenSubKey("SOFTWARE\Microsoft\NET Framework Setup\NDP\")

    For Each versionKeyName In ndpKey.GetSubKeyNames()
        ' Skip .NET Framework 4.5 and later.
        If versionKeyName = "v4" Then Continue For

        If versionKeyName.StartsWith("v") Then
            Dim versionKey As RegistryKey = ndpKey.OpenSubKey(versionKeyName)
            ' Get the .NET Framework version value.
            Dim name = DirectCast(versionKey.GetValue("Version", ""), String)
            ' Get the service pack (SP) number.
            Dim sp = versionKey.GetValue("SP", "").ToString()

            Dim install = versionKey.GetValue("Install", "").ToString()
            If String.IsNullOrEmpty(install) Then
                ' No install info; it must be in a child subkey.
                Console.WriteLine($"{versionKeyName}  {name}")
            ElseIf install = "1" Then

                If Not String.IsNullOrEmpty(sp) Then
                    Console.WriteLine($"{versionKeyName}  {name}  SP{sp}")
                Else
                    Console.WriteLine($"{versionKeyName}  {name}")
                End If

            End If

            If Not String.IsNullOrEmpty(name) Then
                Continue For
            End If

            For Each subKeyName In versionKey.GetSubKeyNames()
                Dim subKey As RegistryKey = versionKey.OpenSubKey(subKeyName)
                name = DirectCast(subKey.GetValue("Version", ""), String)
                If Not String.IsNullOrEmpty(name) Then
                    sp = subKey.GetValue("SP", "").ToString()
                End If

                install = subKey.GetValue("Install", "").ToString()
                If String.IsNullOrEmpty(install) Then
                    ' No install info; it must be later.
                    Console.WriteLine($"{versionKeyName}  {name}")
                ElseIf install = "1" Then

                    If Not String.IsNullOrEmpty(sp) Then
                        Console.WriteLine($"{subKeyName}  {name}  SP{sp}")
                    Else
                        Console.WriteLine($"  {subKeyName}  {name}")
                    End If

                End If
            Next
        End If
    Next
End Using

该示例显示类似于以下内容的输出:The example displays output similar to the following:

v2.0.50727  2.0.50727.4927  SP2
v3.0  3.0.30729.4926  SP2
v3.5  3.5.30729.4926  SP1
v4.0
  Client  4.0.0.0

查找 CLR 版本Find CLR versions

随 .NET Framework 一起安装 .NET Framework CLR 单独进行版本控制。The .NET Framework CLR installed with .NET Framework is versioned separately. 可通过两种方式检测 .NET Framework CLR 的版本:There are two ways to detect the version of the .NET Framework CLR:

  • Clrver 工具The Clrver.exe tool

    使用 CLR 版本工具 (Clrver.exe) 确定计算机上安装了哪些版本的 CLR。Use the CLR Version tool (Clrver.exe) to determine which versions of the CLR are installed on a computer. 打开 Visual Studio 开发人员命令提示并输入 clrverOpen the Developer Command Prompt for Visual Studio and enter clrver.

    示例输出:Sample output:

    Versions installed on the machine:
    v2.0.50727
    v4.0.30319
    
  • EnvironmentThe Environment class

    重要

    对于 .NET Framework 4.5 及更高版本,请勿使用 Environment.Version 属性来检测 CLR 的版本。For .NET Framework 4.5 and later versions, don't use the Environment.Version property to detect the version of the CLR. 而应按照检测 .NET Framework 4.5 及更高版本中所述查询注册表。Instead, query the registry as described in Detect .NET Framework 4.5 and later versions.

    1. 查询 Environment.Version 属性以检索 Version 对象。Query the Environment.Version property to retrieve a Version object.

      返回的 System.Version 对象标识当前正在执行代码的运行时版本。The returned System.Version object identifies the version of the runtime that's currently executing the code. 它不返回可能已安装在计算机上的程序集版本或其他运行时版本。It doesn't return assembly versions or other versions of the runtime that may have been installed on the computer.

      对于 .NET Framework 版本 4、4.5、4.5.1 和 4.5.2,返回的 Version 对象的字符串表示形式为 4.0.30319.xxxxx,其中 xxxxx 小于 42000。For .NET Framework versions 4, 4.5, 4.5.1, and 4.5.2, the string representation of the returned Version object has the form 4.0.30319.xxxxx, where xxxxx is less than 42000. 对于 .NET Framework 4.6 及更高版本,它的格式为 4.0.30319.42000。For .NET Framework 4.6 and later versions, it has the form 4.0.30319.42000.

    2. 获得 Version 对象后,按如下方式查询:After you have the Version object, query it as follows:

      • 对于主要版本标识符(例如,4 表示版本 4.0),请使用 Version.Major 属性。For the major release identifier (for example, 4 for version 4.0), use the Version.Major property.

      • 对于次要版本标识符(例如,0 表示版本 4.0),请使用 Version.Minor 属性For the minor release identifier (for example, 0 for version 4.0), use the Version.Minor property.

      • 对于整个版本字符串(例如,4.0.30319.18010),请使用 Version.ToString 方法。For the entire version string (for example, 4.0.30319.18010), use the Version.ToString method. 此方法返回一个值,该值反映正在执行代码的运行时的版本。This method returns a single value that reflects the version of the runtime that's executing the code. 它不返回可能安装在计算机上的程序集版本或其他运行时版本。It doesn't return assembly versions or other runtime versions that may be installed on the computer.

    以下示例使用 Environment.Version 属性检索 CLR 版本信息:The following example uses the Environment.Version property to retrieve CLR version information:

    Console.WriteLine($"Version: {Environment.Version}");
    
    Console.WriteLine($"Version: {Environment.Version}")
    

    该示例显示类似于以下内容的输出:The example displays output similar to the following:

    Version: 4.0.30319.18010
    

请参阅See also