Version Version Version Version Class

定义

表示程序集、操作系统或公共语言运行时的版本号。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.

只有内部版本号或修订号不同的后续版本的程序集视为可对先前版本的修补程序更新。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 对象通常用于存储有关某个系统或应用程序组件 (如操作系统)、 公共语言运行时,当前应用程序的可执行文件或特定的程序集版本信息。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属性来检索有关公共语言运行时版本信息。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 的浅表副本。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)

适用于