共用方式為


System.Version 類別

本文提供此 API 參考文件的補充備註。

類別 Version 代表元件、操作系統或 Common Language Runtime 的版本號碼。 版本號碼包含兩到四個元件:主要、次要、組建和修訂。 需要主要和次要元件;組建和修訂元件是選擇性的,但如果已定義修訂元件,則需要建置元件。 所有定義的元件都必須是大於或等於 0 的整數。 版本號碼的格式如下(選擇性元件以方括弧顯示):

主要minor[.build[.revision]]

元件會依慣例使用,如下所示:

  • 主要:具有相同名稱但不同主要版本的元件無法互換。 較高的版本號碼可能表示無法假設回溯相容性的產品主要重寫。

  • 次要:如果兩個元件的名稱和主要版本號碼相同,但次要版本號碼不同,這表示具有回溯相容性意圖的重大增強。 這個較高的次要版本號碼可能表示產品的點版本或產品完全回溯相容的新版本。

  • 組建:組建編號的差異代表相同來源的重新編譯。 當處理器、平臺或編譯程式變更時,可能會使用不同的組建編號。

  • 修訂:具有相同名稱、主要和次要版本號碼的元件,但不同的修訂是要完全可互換。 在修正先前發行元件的安全性漏洞的組建中,可能會使用較高的修訂編號。

只有組建或修訂編號不同之元件的後續版本會被視為舊版的 Hotfix 更新。

重要

尚未明確指派值的屬性值為 Version 未定義 (-1)。

MajorRevisionMinorRevision 屬性可讓您識別應用程式的暫存版本,例如,修正問題,直到您可以釋放永久解決方案為止。 此外,Windows NT 操作系統會使用 MajorRevision 屬性來編碼 Service Pack 編號。

將版本資訊指派給元件

一般而言,類別 Version 不會用來將版本號碼指派給元件。 相反地,類別 AssemblyVersionAttribute 會用來定義元件的版本,如本文中的範例所示。

擷取版本資訊

Version 物件最常用來儲存某些系統或應用程式元件的版本資訊(例如作業系統)、Common Language Runtime、目前應用程式的可執行檔或特定元件。 下列範例說明一些最常見的案例:

  • 擷取作業系統版本。 下列範例會使用 OperatingSystem.Version 屬性來擷取作業系統的版本號碼。

    // Get the operating system version.
    OperatingSystem os = Environment.OSVersion;
    Version ver = os.Version;
    Console.WriteLine("Operating System: {0} ({1})", os.VersionString, ver.ToString());
    
    // Get the operating system version.
    let os = Environment.OSVersion
    let ver = os.Version
    printfn $"Operating System: {os.VersionString} ({ver})"
    
    ' Get the operating system version.
    Dim os As OperatingSystem = Environment.OSVersion
    Dim ver As Version = os.Version
    Console.WriteLine("Operating System: {0} ({1})", os.VersionString, ver.ToString())
    
  • 擷取 Common Language Runtime 的版本。 下列範例會使用 Environment.Version 屬性來擷取 Common Language Runtime 的版本資訊。

    // Get the common language runtime version.
    Version ver = Environment.Version;
    Console.WriteLine("CLR Version {0}", ver.ToString());
    
    // Get the common language runtime version.
    let ver = Environment.Version
    printfn $"CLR Version {ver}"
    
    ' Get the common language runtime version.
    Dim ver As Version = Environment.Version
    Console.WriteLine("CLR Version {0}", ver.ToString())
    
  • 擷取目前應用程式的元件版本。 下列範例會 Assembly.GetEntryAssembly 使用 方法來取得對象的參考 Assembly ,該物件表示應用程式可執行檔,然後擷取其元件版本號碼。

    using System;
    using System.Reflection;
    
    public class Example4
    {
       public static void Main()
       {
          // Get the version of the executing assembly (that is, this assembly).
          Assembly assem = Assembly.GetEntryAssembly();
          AssemblyName assemName = assem.GetName();
          Version ver = assemName.Version;
          Console.WriteLine("Application {0}, Version {1}", assemName.Name, ver.ToString());
       }
    }
    
    open System.Reflection
    
    // Get the version of the executing assembly (that is, this assembly).
    let assem = Assembly.GetEntryAssembly()
    let assemName = assem.GetName()
    let ver = assemName.Version
    printfn $"Application {assemName.Name}, Version {ver}"
    
    Imports System.Reflection
    
    Module Example3
        Public Sub Main()
            ' Get the version of the executing assembly (that is, this assembly).
            Dim assem As Assembly = Assembly.GetEntryAssembly()
            Dim assemName As AssemblyName = assem.GetName()
            Dim ver As Version = assemName.Version
            Console.WriteLine("Application {0}, Version {1}", assemName.Name, ver.ToString())
        End Sub
    End Module
    
  • 擷取目前元件的元件版本。 下列範例會使用 Type.Assembly 屬性來取得 對象的參考 Assembly ,該物件表示包含應用程序進入點的元件,然後擷取其版本資訊。

    using System;
    using System.Reflection;
    
    public class Example3
    {
       public static void Main()
       {
          // Get the version of the current assembly.
          Assembly assem = typeof(Example).Assembly;
          AssemblyName assemName = assem.GetName();
          Version ver = assemName.Version;
          Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString());
       }
    }
    
    type Example = class end
    
    // Get the version of the current assembly.
    let assem = typeof<Example>.Assembly
    let assemName = assem.GetName()
    let ver = assemName.Version
    printfn $"{assemName.Name}, Version {ver}"
    
    Imports System.Reflection
    
    Module Example1
        Public Sub Main()
            ' Get the version of the current assembly.
            Dim assem As Assembly = GetType(Example).Assembly
            Dim assemName As AssemblyName = assem.GetName()
            Dim ver As Version = assemName.Version
            Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString())
        End Sub
    End Module
    
  • 擷取特定元件的版本。 下列範例會 Assembly.ReflectionOnlyLoadFrom 使用 方法來取得具有特定檔名的對象參考 Assembly ,然後擷取其版本資訊。 請注意,其他數種方法也存在,以檔名或強名稱具現化 Assembly 物件。

    using System;
    using System.Reflection;
    
    public class Example5
    {
       public static void Main()
       {
          // Get the version of a specific assembly.
          string filename = @".\StringLibrary.dll";
          Assembly assem = Assembly.ReflectionOnlyLoadFrom(filename);
          AssemblyName assemName = assem.GetName();
          Version ver = assemName.Version;
          Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString());
       }
    }
    
    open System.Reflection
    
    // Get the version of a specific assembly.
    let filename = @".\StringLibrary.dll"
    let assem = Assembly.ReflectionOnlyLoadFrom filename
    let assemName = assem.GetName()
    let ver = assemName.Version
    printfn $"{assemName.Name}, Version {ver}"
    
    Imports System.Reflection
    
    Module Example4
        Public Sub Main()
            ' Get the version of a specific assembly.
            Dim filename As String = ".\StringLibrary.dll"
            Dim assem As Assembly = Assembly.ReflectionOnlyLoadFrom(filename)
            Dim assemName As AssemblyName = assem.GetName()
            Dim ver As Version = assemName.Version
            Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString())
        End Sub
    End Module
    
  • 擷取 ClickOnce 應用程式的發行版本。 下列範例會 ApplicationDeployment.CurrentVersion 使用 屬性來顯示應用程式的發行版本。 請注意,其成功執行需要設定範例的應用程式身分識別。 這是由Visual Studio發佈精靈自動處理。

    using System;
    using System.Deployment.Application;
    
    public class Example
    {
       public static void Main()
       {
          Version ver = ApplicationDeployment.CurrentDeployment.CurrentVersion;
          Console.WriteLine("ClickOnce Publish Version: {0}", ver);
       }
    }
    
    Imports System.Deployment.Application
    
    Module Example0
        Public Sub Main()
            Dim ver As Version = ApplicationDeployment.CurrentDeployment.CurrentVersion
            Console.WriteLine("ClickOnce Publish Version: {0}", ver)
        End Sub
    End Module
    

    重要

    ClickOnce 部署之應用程式的發行版本與其元件版本完全無關。

比較版本物件

您可以使用 CompareTo 方法來判斷某個 Version 物件早於、相同或晚於第二 Version 個物件。 下列範例指出 2.1 版晚於 2.0 版。

Version v1 = new Version(2, 0);
Version v2 = new Version("2.1");
Console.Write("Version {0} is ", v1);
switch(v1.CompareTo(v2))
{
   case 0:
      Console.Write("the same as");
      break;
   case 1:
      Console.Write("later than");
      break;
   case -1:
      Console.Write("earlier than");
      break;
}
Console.WriteLine(" Version {0}.", v2);                  
// The example displays the following output:
//       Version 2.0 is earlier than Version 2.1.
open System

let v1 = Version(2, 0)
let v2 = Version "2.1"

printf $"Version {v1} is "

match v1.CompareTo v2 with
| 0 -> printf "the same as"
| 1 -> printf "later than"
| _ -> printf "earlier than"

printf $" Version {v2}."
// The example displays the following output:
//       Version 2.0 is earlier than Version 2.1.
Dim v1 As New Version(2,0)
Dim v2 As New Version("2.1")
Console.Write("Version {0} is ", v1)
Select Case v1.CompareTo(v2)
   Case 0
      Console.Write("the same as")
   Case 1
      Console.Write("later than")
   Case -1
      Console.Write("earlier than")
End Select
Console.WriteLine(" Version {0}.", v2)                  
' The example displays the following output:
'       Version 2.0 is earlier than Version 2.1.

若要讓兩個版本相等,第一個 Version 物件的主要、次要、組建和修訂編號必須與第二個 Version 物件的版本相同。 如果對象的組建或修訂編號 Version 未定義,該 Version 物件會被視為早於 Version 建置或修訂編號等於零的物件。 下列範例會藉由比較具有未定義版本元件的三 Version 個對象來說明這一點。

using System;

enum VersionTime {Earlier = -1, Same = 0, Later = 1 };

public class Example2
{
   public static void Main()
   {
      Version v1 = new Version(1, 1);
      Version v1a = new Version("1.1.0");
      ShowRelationship(v1, v1a);
      
      Version v1b = new Version(1, 1, 0, 0);
      ShowRelationship(v1b, v1a);
   }

   private static void ShowRelationship(Version v1, Version v2)
   {
      Console.WriteLine("Relationship of {0} to {1}: {2}", 
                        v1, v2, (VersionTime) v1.CompareTo(v2));       
   }
}
// The example displays the following output:
//       Relationship of 1.1 to 1.1.0: Earlier
//       Relationship of 1.1.0.0 to 1.1.0: Later
open System

type VersionTime =
    | Earlier = -1
    | Same = 0
    | Later = 1

let showRelationship (v1: Version) (v2: Version) =
    printfn $"Relationship of {v1} to {v2}: {v1.CompareTo v2 |> enum<VersionTime>}" 

let v1 = Version(1, 1)
let v1a = Version "1.1.0"
showRelationship v1 v1a

let v1b = Version(1, 1, 0, 0)
showRelationship v1b v1a

// The example displays the following output:
//       Relationship of 1.1 to 1.1.0: Earlier
//       Relationship of 1.1.0.0 to 1.1.0: Later
Public Enum VersionTime
   Earlier = -1
   Same = 0
   Later = 1
End Enum

Module Example2
    Public Sub Main()
        Dim v1 As New Version(1, 1)
        Dim v1a As New Version("1.1.0")
        ShowRelationship(v1, v1a)

        Dim v1b As New Version(1, 1, 0, 0)
        ShowRelationship(v1b, v1a)
    End Sub

    Private Sub ShowRelationship(v1 As Version, v2 As Version)
        Console.WriteLine("Relationship of {0} to {1}: {2}",
                        v1, v2, CType(v1.CompareTo(v2), VersionTime))
    End Sub
End Module
' The example displays the following output:
'       Relationship of 1.1 to 1.1.0: Earlier
'       Relationship of 1.1.0.0 to 1.1.0: Later