Version 类

定义

表示程序集、操作系统或公共语言运行时的版本号。 此类不能被继承。

public ref class Version sealed : IComparable, IComparable<Version ^>, IEquatable<Version ^>
public ref class Version sealed : ICloneable, IComparable, IComparable<Version ^>, IEquatable<Version ^>, ISpanFormattable, IUtf8SpanFormattable, IUtf8SpanParsable<Version ^>
public ref class Version sealed : ICloneable, IComparable, IComparable<Version ^>, IEquatable<Version ^>
public ref class Version sealed : ICloneable, IComparable, IComparable<Version ^>, IEquatable<Version ^>, ISpanFormattable
public ref class Version sealed : ICloneable, IComparable, IComparable<Version ^>, IEquatable<Version ^>, ISpanFormattable, IUtf8SpanFormattable
public ref class Version sealed : ICloneable, IComparable
public sealed class Version : IComparable, IComparable<Version>, IEquatable<Version>
public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>, ISpanFormattable, IUtf8SpanFormattable, IUtf8SpanParsable<Version>
public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>
public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>, ISpanFormattable
public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>, ISpanFormattable, IUtf8SpanFormattable
[System.Serializable]
public sealed class Version : ICloneable, IComparable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>
type Version = class
    interface IComparable
    interface IComparable<Version>
    interface IEquatable<Version>
type Version = class
    interface ICloneable
    interface IComparable
    interface IComparable<Version>
    interface IEquatable<Version>
    interface IFormattable
    interface ISpanFormattable
    interface IUtf8SpanFormattable
    interface IUtf8SpanParsable<Version>
type Version = class
    interface ICloneable
    interface IComparable
    interface IComparable<Version>
    interface IEquatable<Version>
type Version = class
    interface ICloneable
    interface IComparable
    interface IComparable<Version>
    interface IEquatable<Version>
    interface ISpanFormattable
    interface IFormattable
type Version = class
    interface ICloneable
    interface IComparable
    interface IComparable<Version>
    interface IEquatable<Version>
    interface IFormattable
    interface ISpanFormattable
type Version = class
    interface ICloneable
    interface IComparable
    interface IComparable<Version>
    interface IEquatable<Version>
    interface IFormattable
    interface ISpanFormattable
    interface IUtf8SpanFormattable
type Version = class
    interface IComparable
    interface IComparable<Version>
    interface IEquatable<Version>
    interface ICloneable
[<System.Serializable>]
type Version = class
    interface ICloneable
    interface IComparable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Version = class
    interface ICloneable
    interface IComparable
    interface IComparable<Version>
    interface IEquatable<Version>
Public NotInheritable Class Version
Implements IComparable, IComparable(Of Version), IEquatable(Of Version)
Public NotInheritable Class Version
Implements ICloneable, IComparable, IComparable(Of Version), IEquatable(Of Version), ISpanFormattable, IUtf8SpanFormattable, IUtf8SpanParsable(Of Version)
Public NotInheritable Class Version
Implements ICloneable, IComparable, IComparable(Of Version), IEquatable(Of Version)
Public NotInheritable Class Version
Implements ICloneable, IComparable, IComparable(Of Version), IEquatable(Of Version), ISpanFormattable
Public NotInheritable Class Version
Implements ICloneable, IComparable, IComparable(Of Version), IEquatable(Of Version), ISpanFormattable, IUtf8SpanFormattable
Public NotInheritable Class Version
Implements ICloneable, IComparable
继承
Version
属性
实现

示例

以下示例使用 AssemblyVersionAttribute 特性将版本号分配给程序集。 在编译时,此版本信息随程序集的元数据一起存储。 在运行时,该示例检索程序集中找到的类型上的属性的值Type.Assembly以获取对执行程序集的引用,并从方法返回Version的对象的属性AssemblyName检索程序集的版本信息Assembly.GetName

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.
open System.Reflection

[<assembly: AssemblyVersionAttribute "2.0.1">]
do ()

type Example1 = class end

let thisAssem = typeof<Example1>.Assembly
let thisAssemName = thisAssem.GetName()
   
let ver = thisAssemName.Version
   
printfn $"This is version {ver} of {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 MExample1
    Public Sub Main()
        Dim thisAssem As Assembly = Assembly.GetExecutingAssembly()
        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 类表示程序集、作系统或公共语言运行时的版本号。 版本号由两到四个组件组成:主要、次要、生成和修订。 主要和次要组件是必需的;生成和修订组件是可选的,但如果定义了修订组件,则需要生成组件。 所有定义的组件必须是大于或等于 0 的整数。 版本号的格式如下所示(可选组件以方括号显示):

主要版本号.次要版本号[.内部版本号[.修订号]]

组件按约定使用,如下所示:

  • 主要:名称相同但主要版本不同的组件不可互换。 产品版本号增大可能表明进行了重大重写,这时不能假设存在向后兼容。

  • 次要:如果两个程序集上的名称和主版本号相同,但次要版本号不同,这表示具有向后兼容性的意图显著增强。 此更高的次要版本号可能表示产品的单点版本或产品的完全向后兼容的新版本。

  • 内部版本号:内部版本号的差异表示同一源代码的重新编译。 当处理器、平台或编译器发生更改时,可能会使用不同的内部版本号。

  • 修订版:具有相同名称、主版本号和次要版本号的程序集,但不同的修订旨在完全可互换。 在修复以前发布的程序集中的安全漏洞的内部版本中,可能会使用更高的修订号。

仅内部版本号或修订号不同的程序集的后续版本会被视为前一版本的修补程序更新。

Important

尚未显式分配值的Version属性的值是未定义的(-1)。

使用MajorRevisionMinorRevision属性可以识别您应用程序的临时版本,例如,在可以发布永久解决方案之前用于纠正问题。 此外,Windows NT作系统使用 MajorRevision 属性对 Service Pack 编号进行编码。

将版本信息分配给程序集

通常,类 Version 不用于向程序集分配版本号。 相反,该 AssemblyVersionAttribute 类用于定义程序集的版本,如本文中的示例所示。

检索版本信息

Version 对象最常用于存储有关某些系统或应用程序组件(例如作系统)、公共语言运行时、当前应用程序的可执行文件或特定程序集的版本信息。 以下示例演示了一些最常见的方案:

  • 检索操作系统版本。 以下示例使用 OperatingSystem.Version 属性检索作系统的版本号。

    // Get the operating system version.
    OperatingSystem os = Environment.OSVersion;
    Version ver = os.Version;
    Console.WriteLine($"Operating System: {os.VersionString} ({ver})");
    
    // 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())
    
  • 检索公共语言运行时的版本。 以下示例使用 Environment.Version 属性检索有关公共语言运行时的版本信息。

    // Get the common language runtime version.
    Version ver = Environment.Version;
    Console.WriteLine($"CLR Version {ver}");
    
    // 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(Example3).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 = Assembly.GetExecutingAssembly()
            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
    

比较版本对象

可以使用该方法 CompareTo 来确定一个对象是早于、与另一个 Version 对象相同还是晚于第二 Version 个对象。 以下示例表明版本 2.1 晚于版本 2.0。

Version v1 = new(2, 0);
Version v2 = new("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 {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(1, 1);
      Version v1a = new("1.1.0");
      ShowRelationship(v1, v1a);
      
      Version v1b = new(1, 1, 0, 0);
      ShowRelationship(v1b, v1a);
   }

   private static void ShowRelationship(Version v1, Version v2)
   {
      Console.WriteLine($"Relationship of {v1} to {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

构造函数

名称 说明
Version()

初始化 Version 类的新实例。

Version(Int32, Int32, Int32, Int32)

使用指定的主要、次要、生成和修订号初始化类的新实例 Version

Version(Int32, Int32, Int32)

使用指定的主要、次要值和生成值初始化类的新实例 Version

Version(Int32, Int32)

使用指定的主值和次要值初始化类的新实例 Version

Version(String)

使用指定的字符串初始化类的新实例 Version

属性

名称 说明
Build

获取当前 Version 对象的版本号的生成组件的值。

Major

获取当前 Version 对象的版本号的主要组件的值。

MajorRevision

获取修订号的高 16 位。

Minor

获取当前 Version 对象的版本号的次要组件的值。

MinorRevision

获取修订号的低 16 位。

Revision

获取当前 Version 对象的版本号的修订组件的值。

方法

名称 说明
Clone()

返回一个新 Version 对象,其值与当前 Version 对象相同。

CompareTo(Object)

将当前 Version 对象与指定对象进行比较,并返回其相对值的指示。

CompareTo(Version)

将当前 Version 对象与指定 Version 对象进行比较,并返回其相对值的指示。

Equals(Object)

返回一个值,该值指示当前 Version 对象是否等于指定的对象。

Equals(Version)

返回一个值,该值指示当前 Version 对象和指定的 Version 对象是否表示相同的值。

GetHashCode()

返回当前 Version 对象的哈希代码。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Parse(ReadOnlySpan<Byte>)

将表示版本号的 UTF-8 字符的指定只读范围转换为等效的 Version 对象。

Parse(ReadOnlySpan<Char>)

将表示版本号的指定只读字符范围转换为等效 Version 对象。

Parse(String)

将版本号的字符串表示形式转换为等效 Version 对象。

ToString()

将当前 Version 对象的值转换为其等效 String 表示形式。

ToString(Int32)

将当前 Version 对象的值转换为其等效 String 表示形式。 指定的计数指示要返回的组件数。

TryFormat(Span<Byte>, Int32, Int32)

尝试将此版本实例格式化为字节范围。

TryFormat(Span<Byte>, Int32)

尝试将此版本实例格式化为字节范围。

TryFormat(Span<Char>, Int32, Int32)

尝试将此版本实例格式化为字符范围。

TryFormat(Span<Char>, Int32)

尝试将此版本实例格式化为字符范围。

TryParse(ReadOnlySpan<Byte>, Version)

尝试将版本号的 UTF-8 表示形式转换为等效的 Version 对象,并返回一个值,该值指示转换是否成功。

TryParse(ReadOnlySpan<Char>, Version)

尝试将表示版本号的指定只读字符范围转换为等效 Version 对象,并返回一个值,该值指示转换是否成功。

TryParse(String, Version)

尝试将版本号的字符串表示形式转换为等效 Version 对象,并返回一个值,该值指示转换是否成功。

运营商

名称 说明
Equality(Version, Version)

确定两个指定的 Version 对象是否相等。

GreaterThan(Version, Version)

确定第一个指定 Version 对象是否大于第二个指定 Version 对象。

GreaterThanOrEqual(Version, Version)

确定第一个指定 Version 对象是否大于或等于第二个指定 Version 对象。

Inequality(Version, Version)

确定两个指定的 Version 对象是否不相等。

LessThan(Version, Version)

确定第一个指定 Version 对象是否小于第二个指定 Version 对象。

LessThanOrEqual(Version, Version)

确定第一个指定的 Version 对象是否小于或等于第二个 Version 对象。

显式接口实现

名称 说明
IComparable.CompareTo(Object)

将当前 Version 对象与指定对象进行比较,并返回其相对值的指示。

IFormattable.ToString(String, IFormatProvider)

使用指定格式设置当前实例的值的格式。

ISpanFormattable.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)

尝试将当前实例的值格式化为提供的字符范围。

IUtf8SpanFormattable.TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider)

尝试将当前实例的值格式化为 UTF-8,并将其设置为提供的字节范围。

IUtf8SpanParsable<Version>.Parse(ReadOnlySpan<Byte>, IFormatProvider)

表示程序集、操作系统或公共语言运行时的版本号。 此类不能被继承。

IUtf8SpanParsable<Version>.TryParse(ReadOnlySpan<Byte>, IFormatProvider, Version)

表示程序集、操作系统或公共语言运行时的版本号。 此类不能被继承。

适用于