Version 类

定义

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

主要小调[.生成[。修订号]]major.minor[.build[.revision]]

约定使用组件,如下所示:The components are used by convention as follows:

  • Major:名称相同但主要版本不同的程序集不可互换。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:如果两个程序集的名称和主版本号相同,但次版本号不同,则这表明为了向后兼容,实现了显著的改进。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 开始,MajorRevision 的属性和 MinorRevision 属性使你能够识别应用程序的临时版本,例如,纠正问题,直到可以释放永久解决方案。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 类的新实例。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.

适用于