다음을 통해 공유


Version Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: June 2010

Represents the version number of an assembly, operating system, or the common language runtime. This class cannot be inherited.

Inheritance Hierarchy

System.Object
  System.Version

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public NotInheritable Class Version _
    Implements IComparable, IComparable(Of Version),  _
    IEquatable(Of Version)
[ComVisibleAttribute(true)]
public sealed class Version : IComparable, 
    IComparable<Version>, IEquatable<Version>

The Version type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Version(String) Initializes a new instance of the Version class using the specified string.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Version(Int32, Int32) Initializes a new instance of the Version class using the specified major and minor values.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Version(Int32, Int32, Int32) Initializes a new instance of the Version class using the specified major, minor, and build values.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Version(Int32, Int32, Int32, Int32) Initializes a new instance of the Version class with the specified major, minor, build, and revision numbers.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Build Gets the value of the build component of the version number for the current Version object.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Major Gets the value of the major component of the version number for the current Version object.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Minor Gets the value of the minor component of the version number for the current Version object.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Revision Gets the value of the revision component of the version number for the current Version object.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Clone Returns a new Version object whose value is the same as the current Version object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CompareTo(Object) Compares the current Version object to a specified object and returns an indication of their relative values.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CompareTo(Version) Compares the current Version object to a specified Version object and returns an indication of their relative values.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Returns a value indicating whether the current Version object is equal to a specified object. (Overrides Object.Equals(Object).)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Version) Returns a value indicating whether the current Version object and a specified Version object represent the same value.
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Returns a hash code for the current Version object. (Overrides Object.GetHashCode().)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows Phone Parse Converts the string representation of a version number to an equivalent Version object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString() Converts the value of the current Version object to its equivalent String representation. (Overrides Object.ToString().)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(Int32) Converts the value of the current Version object to its equivalent String representation. A specified count indicates the number of components to return.
Public methodStatic memberSupported by Silverlight for Windows Phone TryParse 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.

Top

Operators

  Name Description
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equality Determines whether two specified Version objects are equal.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GreaterThan Determines whether the first specified Version object is greater than the second specified Version object.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GreaterThanOrEqual Determines whether the first specified Version object is greater than or equal to the second specified Version object.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Inequality Determines whether two specified Version objects are not equal.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 LessThan Determines whether the first specified Version object is less than the second specified Version object.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 LessThanOrEqual Determines whether the first specified Version object is less than or equal to the second Version object.

Top

Remarks

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. 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.

Assigning Version Information to Assemblies

Ordinarily, the Version class is not used to assign a version number to an assembly. Instead, the AssemblyVersionAttribute class is used to define an assembly's version.

Retrieving Version Information

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. The following example uses the OperatingSystem.Version property to retrieve the version number of the operating system.

    ' Get the operating system version.
    Dim os As OperatingSystem = Environment.OSVersion
    Dim ver As Version = os.Version
    outputBlock.Text += String.Format("Operating System: {0} ({1})", os.Platform, ver.ToString()) & vbCrLf
    
    // Get the operating system version.
    OperatingSystem os = Environment.OSVersion;
    Version ver = os.Version;
    outputBlock.Text += String.Format("Operating System: {0} ({1})", os.Platform, ver.ToString()) + "\n";
    
  • Retrieving the version of the 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.
    Dim ver As Version = Environment.Version
    outputBlock.Text += String.Format("CLR Version {0}", ver.ToString()) & vbCrLf
    
    // Get the common language runtime version.
    Version ver = Environment.Version;
    outputBlock.Text += String.Format("CLR Version {0}", ver.ToString()) + "\n";
    

Comparing Version Objects

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. The following example indicates that Version 2.1 is later than Version 2.0.

Dim v1 As New Version(2,0)
Dim v2 As New Version("2.1")
outputBlock.Text += String.Format("Version {0} is ", v1)
Select Case v1.CompareTo(v2)
   Case 0
      outputBlock.Text += "the same as"
   Case 1
      outputBlock.Text += "later than"
   Case -1
      outputBlock.Text += "earlier than"
End Select
outputBlock.Text += String.Format(" Version {0}.", v2) + vbCrLf                 
' The example displays the following output:
'       Version 2.0 is earlier than Version 2.1.
Version v1 = new Version(2, 0);
Version v2 = new Version("2.1");
outputBlock.Text += String.Format("Version {0} is ", v1);
switch(v1.CompareTo(v2))
{
   case 0:
      outputBlock.Text += "the same as";
      break;
   case 1:
      outputBlock.Text += "later than";
      break;
   case -1:
      outputBlock.Text += "earlier than";
      break;
}
outputBlock.Text += String.Format(" Version {0}.\n", v2);                  
// The example displays the following output:
//       Version 2.0 is earlier than Version 2.1.

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. 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. The following example illustrates this by comparing three Version objects that have undefined version components.

Public Enum VersionTime
   Earlier = -1
   Same = 0
   Later = 1
End Enum

Public Module Example
   Dim outputBlock As System.Windows.Controls.TextBlock

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Example.outputBlock = outputBlock

      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)
      outputBlock.Text += String.Format("Relationship of {0} to {1}: {2}", 
                                       v1, v2, CType(v1.CompareTo(v2), VersionTime)) +
                           vbCrLf      
   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
using System;

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

public class Example
{
   static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;

      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)
   {
      outputBlock.Text += String.Format("Relationship of {0} to {1}: {2}\n", 
                                        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

Examples

The following code example uses the Assembly class to obtain the Version object that identifies an assembly.

' This code example demonstrates the Version class.
' 
'   This code example loads an assembly and retrieves its 
'   version number. 
'   Typically, you would use code similar to this example 
'   to load a separate assembly and obtain its version number. 
'   However, solely to avoid using another assembly, this code 
'   example loads itself and retrieves its own version number. 
'
'   This code example is created in two steps:
'1) The AssemblyVersionAttribute attribute is applied to this 
'   code example at the assembly level. Then the code example 
'   is compiled into the MyAssembly.exe executable assembly.
'2) When MyAssembly.exe is executed, it loads itself, then 
'   displays its own version number. 

Imports System.Reflection

' Apply the version number, 1.2.3.4, to this assembly.
<Assembly: AssemblyVersionAttribute("1.2.3.4")> 
Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Use the Assembly class to load MyAssembly.exe. Note that
      ' the name of the assembly does not include the ".exe" suffix
      ' of the executable file.
      Dim myAsm As Assembly = Assembly.Load("MyAssembly")
      Dim aName As AssemblyName = myAsm.GetName()

      ' Store the version number in a Version object.
      Dim ver As Version = aName.Version

      ' Display the version number of MyAssembly.
      outputBlock.Text &= ver.ToString() & vbCrLf
   End Sub 'Main
End Class 'Sample

'This code example produces the following results:
'
'1.2.3.4
'
// This code example demonstrates the Version class.

/* 
   This code example loads an assembly and retrieves its 
   version number. 
   Typically, you would use code similar to this example 
   to load a separate assembly and obtain its version number. 
   However, solely to avoid using another assembly, this code 
   example loads itself and retrieves its own version number. 

   This code example is created in two steps:
1) The AssemblyVersionAttribute attribute is applied to this 
   code example at the assembly level. Then the code example 
   is compiled into the MyAssembly.exe executable assembly.
2) When MyAssembly.exe is executed, it loads itself, then 
   displays its own version number. 
*/

using System;
using System.Reflection;

// Apply the version number, 1.2.3.4, to this assembly.
[assembly: AssemblyVersionAttribute("1.2.3.4")]
class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Use the Assembly class to load MyAssembly.exe. Note that
      // the name of the assembly does not include the ".exe" suffix
      // of the executable file.

      Assembly myAsm = Assembly.Load("MyAssembly");
      AssemblyName aName = myAsm.GetName();

      // Store the version number in a Version object.
      Version ver = aName.Version;

      // Display the version number of MyAssembly.
      outputBlock.Text += ver + "\n";
   }
}

/*
This code example produces the following results:

1.2.3.4

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Change History

Date

History

Reason

June 2010

Added information about comparing Version objects.

Information enhancement.