Version 類別

定義

表示組件的版本號碼、作業系統或 Common Language Runtime。Represents the version number of an assembly, operating system, or the common language runtime. 這個類別無法被繼承。This class cannot be inherited.

public ref class Version sealed : ICloneable, IComparable, IComparable<Version ^>, IEquatable<Version ^>
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>
type Version = class
    interface ICloneable
    interface IComparable
    interface IComparable<Version>
    interface IEquatable<Version>
Public NotInheritable Class Version
Implements ICloneable, IComparable, IComparable(Of Version), IEquatable(Of Version)
繼承
Version
屬性
實作

範例

下列範例會使用 AssemblyVersionAttribute 屬性,將版本號碼指派給元件。The following example uses the AssemblyVersionAttribute attribute to assign a version number to an assembly. 在編譯時期,此版本資訊會與元件的中繼資料一起儲存。At compile time, this version information is stored with the assembly's metadata. 在執行時間,此範例會在元件中找到的類型上抓取 Type.Assembly 屬性的值,以取得執行中元件的參考,並從傳回的 AssemblyName 物件的 Version 屬性中抓取元件的版本資訊。Assembly.GetName 方法。At run time, the example retrieves the value of the Type.Assembly property on a type found in the assembly to get a reference to the executing assembly, and it retrieves the assembly's version information from the Version property of the AssemblyName object returned by the Assembly.GetName method.

using System;
using System.Reflection;

[assembly:AssemblyVersionAttribute("2.0.1")]

public class Example1
{
   public static void Main()
   {
      Assembly thisAssem = typeof(Example1).Assembly;
      AssemblyName thisAssemName = thisAssem.GetName();
       
      Version ver = thisAssemName.Version;
       
      Console.WriteLine("This is version {0} of {1}.", ver, thisAssemName.Name);    
   }
}
// The example displays the following output:
//        This is version 2.0.1.0 of Example1.
Imports System.Reflection

<Assembly:AssemblyVersionAttribute("2.0.1")>
Module Example1
   Public Sub Main()
       Dim thisAssem As Assembly = GetType(Example1).Assembly
       Dim thisAssemName As AssemblyName = thisAssem.GetName()
       
       Dim ver As Version = thisAssemName.Version
       
       Console.WriteLine("This is version {0} of {1}.", ver, thisAssemName.Name)    
   End Sub
End Module
' The example displays the following output:
'      This is version 2.0.1.0 of Example1.

備註

版本號碼是由二到四個元件所組成:主要、次要、組建和修訂。Version numbers consist of two to four components: major, minor, build, and revision. 需要主要和次要元件;組建和修訂元件是選擇性的,但如果定義修訂元件,則需要組建元件。The major and minor components are required; the build and revision components are optional, but the build component is required if the revision component is defined. 所有已定義的元件都必須是大於或等於0的整數。All defined components must be integers greater than or equal to 0. 版本號碼的格式如下(選用元件會顯示在方括弧([和]):The format of the version number is as follows (optional components are shown in square brackets ([ and ]):

主要次要[。build[。修訂]]major.minor[.build[.revision]]

慣例會使用這些元件,如下所示:The components are used by convention as follows:

  • 主要:具有相同名稱但不同主要版本的元件無法互換。Major: Assemblies with the same name but different major versions are not interchangeable. 較高的版本號碼可能表示產品的主要重寫,其無法假設回溯相容性。A higher version number might indicate a major rewrite of a product where backward compatibility cannot be assumed.

  • 次要:如果兩個元件上的名稱和主要版本號碼相同,但次要版本號碼不同,這就表示具有回溯相容性的重大增強功能。Minor: If the name and major version number on two assemblies are the same, but the minor version number is different, this indicates significant enhancement with the intention of backward compatibility. 這個較高的次要版本號碼可能表示產品的點發行,或產品的完全回溯相容新版本。This higher minor version number might indicate a point release of a product or a fully backward-compatible new version of a product.

  • 組建:組建編號的差異代表相同來源的重新編譯。Build: A difference in build number represents a recompilation of the same source. 當處理器、平臺或編譯器變更時,可能會使用不同的組建編號。Different build numbers might be used when the processor, platform, or compiler changes.

  • 修訂:具有相同名稱、主要和次要版本號碼但不同修訂的元件,可供完全互換。Revision: Assemblies with the same name, major, and minor version numbers but different revisions are intended to be fully interchangeable. 在修正先前發行之元件中安全性漏洞的組建中,可能會使用較高的修訂編號。A higher revision number might be used in a build that fixes a security hole in a previously released assembly.

只有組建或修訂編號不同的元件的後續版本會被視為舊版的修正程式更新。Subsequent versions of an assembly that differ only by build or revision numbers are considered to be Hotfix updates of the prior version.

重要

尚未明確指派值之 Version 屬性的值為未定義(-1)。The value of Version properties that have not been explicitly assigned a value is undefined (-1).

從 .NET Framework 2.0 開始,MajorRevisionMinorRevision 屬性可讓您識別應用程式的暫存版本,例如,修正問題,直到您可以發行永久的解決方案為止。Starting with .NET Framework 2.0, the MajorRevision and MinorRevision properties enable you to identify a temporary version of your application that, for example, corrects a problem until you can release a permanent solution. 此外,Windows NT 作業系統會使用 MajorRevision 屬性來編碼 Service Pack 號碼。Furthermore, the Windows NT operating system uses the MajorRevision property to encode the service pack number.

將版本資訊指派給元件Assigning Version Information to Assemblies

一般來說,Version 類別不會用來將版本號碼指派給元件。Ordinarily, the Version class is not used to assign a version number to an assembly. 相反地,會使用 AssemblyVersionAttribute 類別來定義元件的版本,如本主題中的範例所示。Instead, the AssemblyVersionAttribute class is used to define an assembly's version, as illustrated by the example in this topic.

正在抓取版本資訊Retrieving Version Information

Version 物件最常用來儲存一些系統或應用程式元件(例如作業系統)、common language runtime、目前應用程式的可執行檔或特定元件的版本資訊。Version objects are most frequently used to store version information about some system or application component (such as the operating system), the common language runtime, the current application's executable, or a particular assembly. 下列範例說明一些最常見的案例:The following examples illustrate some of the most common scenarios:

  • 正在抓取作業系統版本。Retrieving the operating system version. 下列範例會使用 OperatingSystem.Version 屬性來取出作業系統的版本號碼。The following example uses the OperatingSystem.Version property to retrieve the version number of the operating system.

    // 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.
    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 的版本。Retrieving the version of the common language runtime. 下列範例會使用 Environment.Version 屬性來抓取有關 common language runtime 的版本資訊。The following example uses the Environment.Version property to retrieve version information about the 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.
    Dim ver As Version = Environment.Version
    Console.WriteLine("CLR Version {0}", ver.ToString())
    
  • 正在抓取目前應用程式的元件版本。Retrieving the current application's assembly version. 下列範例會使用 Assembly.GetEntryAssembly 方法來取得代表應用程式可執行檔之 Assembly 物件的參考,然後再抓取其元件版本號碼。The following example uses the Assembly.GetEntryAssembly method to obtain a reference to an Assembly object that represents the application executable and then retrieves its assembly version number.

    using System;
    using System.Reflection;
    
    public class Example
    {
       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());
       }
    }
    
    Imports System.Reflection
    
    Module Example
       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
    
  • 正在抓取目前元件的元件版本。Retrieving the current assembly's assembly version. 下列範例會使用 Type.Assembly 屬性來取得代表包含應用程式進入點之元件的 Assembly 物件的參考,然後再抓取其版本資訊。The following example uses the Type.Assembly property to obtain a reference to an Assembly object that represents the assembly that contains the application entry point, and then retrieves its version information.

    using System;
    using System.Reflection;
    
    public class Example
    {
       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());
       }
    }
    
    Imports System.Reflection
    
    Module Example
       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())            VB
       End Sub
    End Module
    
  • 正在抓取特定元件的版本。Retrieving the version of a specific assembly. 下列範例會使用 Assembly.ReflectionOnlyLoadFrom 方法來取得具有特定檔案名之 Assembly 物件的參考,然後再抓取其版本資訊。The following example uses the Assembly.ReflectionOnlyLoadFrom method to obtain a reference to an Assembly object that has a particular file name, and then retrieves its version information. 請注意,有數個其他方法也存在,依檔案名或強式名稱具現化 Assembly 物件。Note that several other methods also exist to instantiate an Assembly object by file name or by strong name.

    using System;
    using System.Reflection;
    
    public class Example
    {
       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());
       }
    }
    
    Imports System.Reflection
    
    Module Example
       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 應用程式的發行版本。Retrieving the Publish Version of a ClickOnce application. 下列範例會使用 ApplicationDeployment.CurrentVersion 屬性來顯示應用程式的發行版本。The following example uses the ApplicationDeployment.CurrentVersion property to display an application's Publish Version. 請注意,它的執行成功需要設定範例的應用程式身分識別。Note that its successful execution requires the example's application identity to be set. 這會由 Visual Studio 發佈嚮導自動處理。This is handled automatically by the Visual Studio Publish Wizard.

    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 Example
       Public Sub Main()
          Dim ver As Version = ApplicationDeployment.CurrentDeployment.CurrentVersion
          Console.WriteLine("ClickOnce Publish Version: {0}", ver)
       End Sub
    End Module
    

    重要

    ClickOnce 部署應用程式的發行版本完全獨立于其元件版本。The Publish Version of an application for ClickOnce deployment is completely independent of its assembly version.

比較版本物件Comparing Version Objects

您可以使用 CompareTo 方法來判斷一個 Version 物件早于、同于或晚于第二個 Version 物件。You can use the CompareTo method to determine whether one Version object is earlier than, the same as, or later than a second Version object. 下列範例表示2.1 版晚于2.0 版。The following example indicates that Version 2.1 is later than Version 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.
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 物件的相同。For two versions to be equal, the major, minor, build, and revision numbers of the first Version object must be identical to those of the second Version object. 如果未定義 Version 物件的組建或修訂編號,則會將該 Version 物件視為早于其組建或修訂編號等於零的 Version 物件。If the build or revision number of a Version object is undefined, that Version object is considered to be earlier than a Version object whose build or revision number is equal to zero. 下列範例藉由比較三個具有未定義版本元件的 Version 物件來說明這一點。The following example illustrates this by comparing three Version objects that have undefined version components.

using System;

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

public class Example
{
   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
Public Enum VersionTime
   Earlier = -1
   Same = 0
   Later = 1
End Enum

Module Example
   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

建構函式

Version()

初始化 Version 類別的新執行個體。Initializes a new instance of the Version class.

Version(Int32, Int32)

使用指定的主要和次要值,初始化 Version 類別的新執行個體。Initializes a new instance of the Version class using the specified major and minor values.

Version(Int32, Int32, Int32)

使用指定的主要、次要和組建值,初始化 Version 類別的新執行個體。Initializes a new instance of the Version class using the specified major, minor, and build values.

Version(Int32, Int32, Int32, Int32)

使用指定的主要、次要、組建和修訂編號,初始化 Version 類別的新執行個體。Initializes a new instance of the Version class with the specified major, minor, build, and revision numbers.

Version(String)

以指定的字串初始化 Version 類別的新執行個體。Initializes a new instance of the Version class using the specified string.

屬性

Build

取得目前 Version 物件的版本號碼之組建元件值。Gets the value of the build component of the version number for the current Version object.

Major

取得目前 Version 物件的版本號碼之主要元件值。Gets the value of the major component of the version number for the current Version object.

MajorRevision

取得修訂編號的高 16 位元。Gets the high 16 bits of the revision number.

Minor

取得目前 Version 物件的版本號碼之次要元件值。Gets the value of the minor component of the version number for the current Version object.

MinorRevision

取得修訂編號的低 16 位元。Gets the low 16 bits of the revision number.

Revision

取得目前 Version 物件的版本號碼之修訂元件值。Gets the value of the revision component of the version number for the current Version object.

方法

Clone()

傳回新的 Version 物件,它的值和目前的 Version 物件相同。Returns a new Version object whose value is the same as the current Version object.

CompareTo(Object)

將目前 Version 物件與指定物件相比較,並傳回其相對值的指示。Compares the current Version object to a specified object and returns an indication of their relative values.

CompareTo(Version)

將目前 Version 物件與指定的 Version 物件相比較,並傳回其相對值的指示。Compares the current Version object to a specified Version object and returns an indication of their relative values.

Equals(Object)

傳回值,指出目前的 Version 物件是否等於指定的物件。Returns a value indicating whether the current Version object is equal to a specified object.

Equals(Version)

傳回值,指出目前的 Version 物件和指定的 Version 物件是否表示相同的值。Returns a value indicating whether the current Version object and a specified Version object represent the same value.

GetHashCode()

傳回目前 Version 物件的雜湊碼。Returns a hash code for the current Version object.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
Parse(ReadOnlySpan<Char>)

轉換代表相當於 Version 物件版本號碼的指定字元唯讀延伸。Converts the specified read-only span of characters that represents a version number to an equivalent Version object.

Parse(String)

將版本號碼的字串表示轉換為對等的 Version 物件。Converts the string representation of a version number to an equivalent Version object.

ToString()

將目前 Version 物件的值轉換為它的對等 String 表示。Converts the value of the current Version object to its equivalent String representation.

ToString(Int32)

將目前 Version 物件的值轉換為它的對等 String 表示。Converts the value of the current Version object to its equivalent String representation. 指定的計數指示要傳回的元件數目。A specified count indicates the number of components to return.

TryFormat(Span<Char>, Int32)

嘗試將此版本執行個體格式化為字元延伸。Tries to format this version instance into a span of characters.

TryFormat(Span<Char>, Int32, Int32)

嘗試將此版本執行個體格式化為字元延伸。Tries to format this version instance into a span of characters.

TryParse(ReadOnlySpan<Char>, Version)

嘗試轉換代表相當於 Version 物件版本號碼的指定唯讀字元延伸,且傳回指出轉換是否成功的值。Tries to convert the specified read-only span of characters representing a version number to an equivalent Version object, and returns a value that indicates whether the conversion succeeded.

TryParse(String, Version)

嘗試將版本號碼的字串表示轉換為對等的 Version 物件,並傳回表示轉換是否成功的值。Tries to convert the string representation of a version number to an equivalent Version object, and returns a value that indicates whether the conversion succeeded.

運算子

Equality(Version, Version)

判斷指定的兩個 Version 物件是否相等。Determines whether two specified Version objects are equal.

GreaterThan(Version, Version)

判斷第一個指定的 Version 物件是否大於第二個指定的 Version 物件。Determines whether the first specified Version object is greater than the second specified Version object.

GreaterThanOrEqual(Version, Version)

判斷第一個指定的 Version 物件是否大於或等於第二個指定的 Version 物件。Determines whether the first specified Version object is greater than or equal to the second specified Version object.

Inequality(Version, Version)

判斷指定的兩個 Version 物件是否相等。Determines whether two specified Version objects are not equal.

LessThan(Version, Version)

判斷第一個指定的 Version 物件是否小於第二個指定的 Version 物件。Determines whether the first specified Version object is less than the second specified Version object.

LessThanOrEqual(Version, Version)

判斷第一個指定的 Version 物件是否小於或等於第二個 Version 物件。Determines whether the first specified Version object is less than or equal to the second Version object.

明確介面實作

IComparable.CompareTo(Object)

將目前 Version 物件與指定物件相比較,並傳回其相對值的指示。Compares the current Version object to a specified object and returns an indication of their relative values.

適用於