作法:判斷安裝的 .NET Framework 版本How to: Determine which .NET Framework versions are installed

使用者可以在電腦上安裝及執行多個版本的 .NET Framework。Users can install and run multiple versions of the .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 .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 和組件會共用相同的版本號碼。The .NET Framework and assemblies share the same version number.

  • 通用語言執行平台 (CLR),負責管理和執行應用程式的程式碼。The common language runtime (CLR), which manages and executes your app's code. CLR 是透過自己的版本號碼加以識別 (請參閱版本和相依性)。The CLR is identified by its own version number (see Versions and Dependencies).

注意

每一個新的 .NET Framework 版本都會保留舊版的功能並增加新的功能。Each new version of the .NET Framework retains features from the previous versions and adds new features. 您可同時在一部電腦上載入多個版本的 .NET Framework,這表示您可以安裝 .NET Framework,卻不必解除安裝舊版。You can load multiple versions of the .NET Framework on a single computer at the same time, which means that you can install the .NET Framework without having to uninstall previous versions. 一般而言,您不應該解除安裝舊版的 .NET Framework,因為您使用的應用程式可能依賴某個特定版本,而若移除該版本則應用程式可能會中斷。In general, you shouldn't uninstall previous versions of the .NET Framework, because an application you use may depend on a specific version and may break if that version is removed.

.NET Framework 版本和 CLR 版本之間有差異:There is a difference between the .NET Framework version and the CLR version:

  • .NET Framework 版本是以構成 .NET Framework Class Library 的組件集為基礎。The .NET Framework version is based on the set of assemblies that form the .NET Framework class library. 例如,.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 版本是以 .NET Framework 應用程式執行所在的執行階段為基礎。The CLR version is based on the runtime on which .NET Framework applications execute. 單一的 CLR 版本通常會支援多個 .NET Framework 版本。A single CLR version typically supports multiple .NET Framework versions. 例如,CLR 4.0.30319.xxxxx 版支援 .NET Framework 4 到 4.5.2 版,其中 xxxxx 會小於 42000,而 CLR 4.0.30319.42000 版支援從 .NET Framework 4.6 起的 .NET Framework 版本。For example, CLR version 4.0.30319.xxxxx supports .NET Framework versions 4 through 4.5.2, where xxxxx is less than 42000, and CLR version 4.0.30319.42000 supports .NET Framework versions starting with .NET Framework 4.6.

如需版本的詳細資訊,請參閱 .NET Framework 版本和相依性For more information about versions, see .NET Framework versions and dependencies.

您可以存取登錄來取得電腦上安裝的 .NET Framework 版本清單。To get a list of the .NET Framework versions installed on a computer, you access the registry. 您可以使用登錄編輯程式來檢視登錄,或使用程式碼來查詢登錄:You can either use the Registry Editor to view the registry or use code to query it:

使用工具或程式碼取得安裝在電腦上的 CLR 版本清單:To get a list of the CLR versions installed on a computer, use a tool or code:

如需偵測每一版 .NET Framework 已安裝更新的資訊,請參閱如何:判斷已安裝的 .NET Framework 更新For information about detecting the installed updates for each version of the .NET Framework, see How to: Determine which .NET Framework updates are installed.

尋找新版的 .NET Framework (4.5 和更新版本)Find newer .NET Framework versions (4.5 and later)

在登錄中尋找 .NET Framework 4.5 和更新版本Find .NET Framework versions 4.5 and later in the registry

  1. 從 [開始] 功能表上,選擇 [執行] ,輸入 regedit,然後選取 [確定] 。From the Start menu, choose Run, enter regedit, and then select OK.

    您必須具有系統管理認證才能執行 regedit。You must have administrative credentials to run regedit.

  2. 在 [登錄編輯程式] 中,開啟下列子機碼:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\FullIn 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 the .NET Framework 4.5 or later installed.

    注意

    登錄中的 NET Framework Setup 資料夾「不是」 以英文句號開頭。The NET Framework Setup folder in the registry does not begin with a period.

  3. 檢查是否有名為 Release 的 DWORD 項目。Check for a DWORD entry named Release. 若有,則表示已安裝 .NET Framework 4.5 或更新版本。If it exists, then you have .NET Framework 4.5 or later versions installed. 其值為對應至 .NET Framework 特定版本的版本機碼。Its value is a release key that corresponds to a particular version of the .NET Framework. 例如,下圖中 Release 項目的值是 378389,也就是 .NET Framework 4.5 的版本機碼。In the following figure, for example, the value of the Release entry is 378389, which is the release key for .NET Framework 4.5.

    .NET Framework 4.5 的登錄項目Registry entry for the .NET Framework 4.5

下表列出 .NET Framework 4.5 及更新版本個別作業系統上 Release DWORD 的值。The following table lists the value of the Release DWORD on individual operating systems for .NET Framework 4.5 and later versions.

重要

下表僅列出 .NET Framework 發行版本的索引碼。The following table lists the keys of released versions of the .NET Framework only. 它不會列出預覽或發行前版本的索引碼。It doesn't list the keys of preview or pre-release versions.

.NET Framework 版本.NET Framework version Release DWORD 的值Value of the Release DWORD
.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 Creators Update 上: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 版以外的所有 OS 版本上: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 月更新上:528040On Windows 10 May 2019 Update: 528040
在其他所有 Windows 作業系統 (包括其他 Windows 10 作業系統) 上:528049On all others Windows operating systems (including other Windows 10 operating systems): 528049

您可以依照下列方式使用這些值:You can use these values as follows:

  • 若要判斷特定版本 Windows 作業系統上是否安裝特定版本的 .NET Framework,請測試表格中所列 Release DWORD 值是否「等於」 表格中所列的值。To determine whether a specific version of the .NET Framework is installed on a particular version of the Windows operating system, test whether the Release DWORD value is equal to the value listed in the table. 例如,若要判斷 Windows 10 系統上是否有 .NET Framework 4.6,請測試「等於」 393295 的 Release 值。For example, to determine whether .NET Framework 4.6 is present on a Windows 10 system, test for the a Release value that is equal to 393295.

  • 若要判斷 .NET Framework 最低版本是否存在,請使用該版本較小的 RELEASEDWORD 值。To determine whether a minimum version of the .NET Framework is present, use the smaller RELEASE DWORD value for that version. 例如,如果您的應用程式是執行.NET Framework 4.6 或更新版本,請測試「大於或等於」 393295 的 RELEASE DWORD 值。For example, if your application runs under .NET Framework 4.6 or a later version, test for a RELEASE DWORD value that is greater than or equal to 393295. 對於只列出每一 .NET Framework 版本最小 RELEASE DWORD 值的資料表,請參閱 .NET Framework 4.5 與更新版本的最小 Release DWORD 值.For a table that lists only the minimum RELEASE DWORD value for each .NET Framework version, see The minimum values of the Release DWORD for .NET Framework 4.5 and later versions.

  • 若要測試多個版本,請從測試「大於或等於」 最新版 .NET Framework 的較小 DWORD 值開始,然後將值與每一後續較早版本的較小 DWORD 值進行比較。To test for multiple versions, begin by testing for a value that is greater than or equal to the smaller DWORD value for the latest .NET Framework version, and then compare the value with the smaller DWORD value for each successive earlier version. 例如,如果應用程式需要 .NET Framework 4.7 或更新版本,而且您想要判斷現有 .NET Framework 的特定版本,請從測試「大於或等於」 461808 (.NET Framework 4.7.2 的較小 DWORD 值) 的 RELEASE DWORD 值開始。For example, if your application requires .NET Framework 4.7 or later and you want to determine the specific version of .NET Framework present, start by testing for a RELEASE DWORD value that is great than or equal to to 461808 (the smaller DWORD value for .NET Framework 4.7.2). 然後將 RELEASE DWORD 值與每一更新版本 .NET Framework 的較小值進行比較。Then compare the RELEASE DWORD value with the smaller value for each later .NET Framework version. 對於只列出每一 .NET Framework 版本最小 RELEASE DWORD 值的資料表,請參閱 .NET Framework 4.5 與更新版本的最小 Release DWORD 值.For a table that lists only the minimum RELEASE DWORD value for each .NET Framework version, see The minimum values of the Release DWORD for .NET Framework 4.5 and later versions.

使用程式碼尋找 .NET Framework 4.5 和更新版本Find .NET Framework versions 4.5 and later with 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.

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full 子機碼中存在 Release DWORD 項目表示 .NET Framework 4.5 或更新版本已安裝在電腦上。The existence of the Release DWORD entry in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full subkey indicates that the .NET Framework 4.5 or a later version is installed on a computer.

  2. 檢查 Release 項目的值,判斷已安裝的版本。Check the value of the Release entry 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 和更新版本:The following example checks the value of the Release entry in the registry to find the .NET Framework 4.5 and later versions that are installed:

using System;
using Microsoft.Win32;

public class GetDotNetVersion
{
   public static void Main()
   {
      Get45PlusFromRegistry();
   }

   private static void Get45PlusFromRegistry()
   {
      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.
      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";
      }
   }
}   
// This example displays output like the following:
//       .NET Framework Version: 4.6.1
Imports Microsoft.Win32

Public Module GetDotNetVersion
   Public Sub Main()
      Get45PlusFromRegistry()
   End Sub

   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 
            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 >= will enable forward compatibility.
   Private Function CheckFor45PlusVersion(releaseKey As Integer) As String
      If releaseKey >= 528040 Then
         Return "4.8 or later"
      Else If releaseKey >= 461808 Then
         Return "4.7.2" 
      Else If releaseKey >= 461308 Then
         Return "4.7.1"
      Else If releaseKey >= 460798 Then
         Return "4.7"
      Else If releaseKey >= 394802 Then
         Return "4.6.2"
      Else If releaseKey >= 394254 Then 
         Return "4.6.1"
      Else If releaseKey >= 393295 Then
         Return "4.6"
      Else If releaseKey >= 379893 Then
         Return "4.5.2"
      Else If releaseKey >= 378675 Then
         Return "4.5.1"
      Else If 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
End Module   
' 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.

使用 PowerShell 檢查 .NET Framework 最低版本需求 (4.5 及更新版本)Check for a minimum-required .NET Framework version (4.5 and later) with PowerShell

  • 使用 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 the .NET Framework 4.6.2 or later is installed. 如已安裝,則此程式碼會傳回 True;否則傳回 FalseThis code returns True if it's installed and False otherwise.

# PowerShell 5
 Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\' |  Get-ItemPropertyValue -Name Release | Foreach-Object { $_ -ge 394802 }
# PowerShell 4
(Get-ItemProperty "HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full").Release -ge 394802

若要檢查不同的所需 .NET Framework 最低版本,請以 .NET Framework 版本表Release 值取代這些範例中的 394802To check for a different minimum-required .NET Framework version, replace 394802 in these examples with a Release value from the .NET Framework version table.

尋找舊版的 .NET Framework (1–4)Find older .NET Framework versions (1–4)

在登錄中尋找 .NET Framework 1–4 版Find .NET Framework versions 1–4 in the registry

  1. 從 [開始] 功能表上,選擇 [執行] ,輸入 regedit,然後選取 [確定] 。From the Start menu, choose Run, enter regedit, and then select OK.

    您必須具有系統管理認證才能執行 regedit。You must have administrative credentials to run regedit.

  2. 在 [登錄編輯程式] 中,開啟下列子機碼:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDPIn the Registry Editor, open the following subkey: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP:

    • 針對 .NET Framework 1.1 到 3.5 版,每個安裝的版本都會列為 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP 子機碼下的子機碼。For .NET Framework versions 1.1 through 3.5, each installed version is listed as a subkey under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP subkey. 例如,HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5For example, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5. 版本號碼儲存為版本子機碼 Version 項目中的值。The version number is stored as a value in the version subkey's Version entry.

    • 以 .NET Framework 4 為例,Version 項目位在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4.0\Client 及/或 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4.0\Full 子機碼下。For .NET Framework 4, the Version entry is under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4.0\Client subkey, the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4.0\Full subkey, or under both subkeys.

    注意

    登錄中的 NET Framework Setup 資料夾不是以英文句號開頭。The NET Framework Setup folder in the registry does not begin with a period.

    下圖顯示 .NET Framework 3.5 的子機碼及其 Version 項目。The following figure shows the subkey and its Version entry for the .NET Framework 3.5.

    .NET Framework 3.5 的登錄項目。The registry entry for the .NET Framework 3.5.

使用程式碼尋找 .NET Framework 1–4 版Find .NET Framework versions 1–4 with code

  • 使用 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.

下列範例會尋找已安裝的 .NET Framework 1–4 版:The following example finds the .NET Framework 1–4 versions that are installed:

using Microsoft.Win32;
using System;

public static class VersionTest
{
    public static void Main()
    {
        GetVersionFromRegistry();
    }
    
    private static void GetVersionFromRegistry()
    {
        // Opens 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, or an empty string if there is none.
                    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 (!(string.IsNullOrEmpty(sp)) && install == "1")
                        {
                            Console.WriteLine($"{versionKeyName}  {name}  SP{sp}");
                        }
                    }
                    if (! string.IsNullOrEmpty(name))
                    {
                        continue;
                    }
                    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 (!(string.IsNullOrEmpty(sp )) && install == "1")
                            {
                                Console.WriteLine($"{subKeyName}  {name}  SP{sp}");
                            }
                            else if (install == "1")
                            {
                                Console.WriteLine($"  {subKeyName}  {name}");
                            }
                        }
                    }
                }
            }
        }
    }
}
// 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
Imports Microsoft.Win32

Public Module VersionTest
    Public Sub Main()
        GetVersionFromRegistry()
    End Sub
    
    Private Sub GetVersionFromRegistry()

        ' 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}")
                    Else
                        If Not String.IsNullOrEmpty(sp) AndAlso install = "1" Then
                            Console.WriteLine($"{versionKeyName}  {name}  SP{sp}")
                        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}")
                        Else
                            If Not String.IsNullOrEmpty(sp) AndAlso install = "1" Then
                                Console.WriteLine($"{subKeyName}  {name}  SP{sp}")
                            ElseIf install = "1" Then
                                Console.WriteLine($"  {subKeyName}  {name}")
                            End If
                        End If
                    Next
                End If
            Next
        End Using
    End Sub
End Module
' 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

使用 Clrver.exe 尋找目前的 CLR 版本Find the current CLR version with Clrver.exe

使用 CLR 版本工具 (Clrver.exe) 以判斷電腦上安裝了哪些 CLR 版本:Use the CLR Version tool (Clrver.exe) to determine which versions of the CLR are installed on a computer:

使用環境類別尋找目前的 CLR 版本Find the current CLR version with the Environment class

重要

針對 .NET Framework 4.5 和更新版本,請不要使用 Environment.Version 屬性偵測 CLR 的版本。For the .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 Find .NET Framework versions 4.5 and later with code.

  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 the .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 the .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.0 版的 4),請使用 Version.Major 屬性。For the major release identifier (for example, 4 for version 4.0), use the Version.Major property.

    • 針對次要版本識別項 (例如 4.0 版的 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:

using System;

public class VersionTest
{
    public static void Main()
    {
        Console.WriteLine($"Version: {Environment.Version}");
    }
}
// The example displays output similar to the following:'
//    Version: 4.0.30319.18010
Imports Microsoft.Win32

Public Module VersionTest
   Public Sub Main()
      GetVersionFromEnvironment()
   End Sub
   
    Private Sub GetVersionFromEnvironment()
        Console.WriteLine($"Version: {Environment.Version}")
    End Sub
End Module
' The example displays output similiar to the following:'
'    Version: 4.0.30319.18010

另請參閱See also