Version Version Version Version Class

定義

表示組件的版本號碼、作業系統或 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)
繼承
VersionVersionVersionVersion
屬性
實作

範例

下列範例會使用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以取得執行的組件的參考組件中找到的型別上的屬性,而且會擷取從組件的版本資訊Version屬性AssemblyName所傳回的物件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 ]):

主要次要[。建置[。修訂]]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.

後續版本的組件不同的組建或修訂的數字只會被視為舊版本的 Hotfix 更新。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.0MajorRevisionMinorRevision屬性可讓您識別您的應用程式,比方說,更正問題,直到您可以釋放永久解決方案的暫存版本。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 物件最常用來儲存某些系統或應用程式元件 (例如作業系統)、 通用語言執行平台、 目前的應用程式可執行檔或特定組件的版本資訊。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())
    
  • 擷取通用語言執行平台的版本。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() Version() Version()

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

Version(Int32, Int32) Version(Int32, Int32) Version(Int32, Int32) Version(Int32, Int32)

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

Version(Int32, Int32, Int32) Version(Int32, Int32, Int32) Version(Int32, Int32, Int32) 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(Int32, Int32, Int32, Int32) Version(Int32, Int32, Int32, Int32) 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(String) Version(String) Version(String)

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

屬性

Build Build Build Build

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

Major Major Major Major

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

MajorRevision MajorRevision MajorRevision MajorRevision

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

Minor Minor Minor Minor

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

MinorRevision MinorRevision MinorRevision MinorRevision

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

Revision Revision Revision Revision

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

方法

Clone() Clone() Clone() Clone()

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

CompareTo(Object) CompareTo(Object) CompareTo(Object) CompareTo(Object)

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

CompareTo(Version) CompareTo(Version) CompareTo(Version) CompareTo(Version)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

Equals(Version) Equals(Version) Equals(Version) Equals(Version)

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

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>)
Parse(String) Parse(String) Parse(String) Parse(String)

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

ToString() ToString() ToString() ToString()

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

ToString(Int32) ToString(Int32) ToString(Int32) 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) TryFormat(Span<Char>, Int32) TryFormat(Span<Char>, Int32) TryFormat(Span<Char>, Int32)
TryFormat(Span<Char>, Int32, Int32) TryFormat(Span<Char>, Int32, Int32) TryFormat(Span<Char>, Int32, Int32) TryFormat(Span<Char>, Int32, Int32)
TryParse(ReadOnlySpan<Char>, Version) TryParse(ReadOnlySpan<Char>, Version) TryParse(ReadOnlySpan<Char>, Version) TryParse(ReadOnlySpan<Char>, Version)
TryParse(String, Version) TryParse(String, Version) TryParse(String, Version) 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) Equality(Version, Version) Equality(Version, Version) Equality(Version, Version)

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

GreaterThan(Version, Version) GreaterThan(Version, Version) GreaterThan(Version, Version) GreaterThan(Version, Version)

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

GreaterThanOrEqual(Version, Version) GreaterThanOrEqual(Version, Version) GreaterThanOrEqual(Version, Version) 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) Inequality(Version, Version) Inequality(Version, Version) Inequality(Version, Version)

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

LessThan(Version, Version) LessThan(Version, Version) LessThan(Version, Version) LessThan(Version, Version)

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

LessThanOrEqual(Version, Version) LessThanOrEqual(Version, Version) LessThanOrEqual(Version, Version) 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) IComparable.CompareTo(Object) IComparable.CompareTo(Object) IComparable.CompareTo(Object)

適用於