Version Class
.NET Framework Class Library
Version Class

Updated: July 2008

Represents the version number for a common language runtime assembly. This class cannot be inherited.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class Version _
    Implements ICloneable, IComparable, IComparable(Of Version),  _
    IEquatable(Of Version)
Visual Basic (Usage)
Dim instance As Version
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class Version : ICloneable, 
    IComparable, IComparable<Version>, IEquatable<Version>
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class Version sealed : ICloneable, 
    IComparable, IComparable<Version^>, IEquatable<Version^>
JScript
public final class Version implements ICloneable, IComparable, IComparable<Version>, IEquatable<Version>

Use a Version object to store and compare the version number of an assembly. Note that you or an application can set a Version object to the version number of an assembly. However, a Version object is not automatically set to the version number of any particular assembly, and the Version class has no members that can acquire such information.

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. This would be appropriate, for example, for a major rewrite of a product where backward compatibility cannot be assumed.

  • Minor : If the name and major number on two assemblies are the same, but the minor number is different, this indicates significant enhancement with the intention of backward compatibility. This would be appropriate, for example, on 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. This would be appropriate because of 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. This would be appropriate to fix 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.

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. Furthermore, the Windows NT operating system uses the MajorRevision property to encode the service pack number.

This class implements the ICloneable, IComparable, IComparable<(Of <(T>)>), and IEquatable<(Of <(T>)>) interfaces.

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.

    Visual Basic
    ' 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())
    
    C#
    // Get the operating system version.
    OperatingSystem os = Environment.OSVersion;
    Version ver = os.Version;
    Console.WriteLine("Operating System: {0} ({1})", os.VersionString, ver.ToString());
    
  • 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.

    Visual Basic
    ' Get the common language runtime version.
    Dim ver As Version = Environment.Version
    Console.WriteLine("CLR Version {0}", ver.ToString())
    
    C#
    // Get the common language runtime version.
    Version ver = Environment.Version;
    Console.WriteLine("CLR Version {0}", ver.ToString());
    
  • Retrieving the current application's version. 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 version number.

    Visual Basic
    ' 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())
    
    C#
    // 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());
    
  • Retrieving the current assembly's version. The following example uses the Assembly..::.GetExecutingAssembly method to obtain a reference to an Assembly object that represents the current assembly and then retrieves its version information.

    Visual Basic
    ' Get the version of the current application.
    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())
    
    C#
    // Get the version of the current application.
    Assembly assem = Assembly.GetExecutingAssembly();
    AssemblyName assemName = assem.GetName();
    Version ver = assemName.Version;
    Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString());
    
  • Retrieving the version of a specific 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. Note that several other methods also exist to instantiate an Assembly object by file name or by strong name.

    Visual Basic
    ' 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())
    
    C#
    // 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());
    

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

Visual Basic
' 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
Imports System.Reflection

' Apply the version number, 1.2.3.4, to this assembly.
<assembly:AssemblyVersionAttribute("1.2.3.4")>
Class Sample
    Public Shared Sub Main() 

' 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.
        Console.WriteLine(ver)
    End Sub 'Main
End Class 'Sample

'This code example produces the following results:
'
'1.2.3.4
'
C#
// 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 Sample 
{
    public static void Main() 
    {
// 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.
    Console.WriteLine(ver);
    }
}

/*
This code example produces the following results:

1.2.3.4

*/
System..::.Object
  System..::.Version
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

July 2008

Added sections on assigning and retrieving version information to the Remarks section.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
'2.0' != '2.0.0.0'      David M. Kean - MSFT   |   Edit   |   Show History
One thing to be aware of, is that a version representing '2.0' does not equal a version representing '2.0.0.0'.

The following example shows this:

[C#]
using System;


namespace Samples
{
class Program
{
static void Main(string[] args)
{
Version version1 = new Version(2, 0);
Version version2 = new Version(2, 0, 0);
Version version3 = new Version(2, 0, 0, 0);
Console.WriteLine(version1.Equals(version2));
Console.WriteLine(version1.Equals(version3));
Console.WriteLine(version2.Equals(version3));
}
}
}

The above example outputs the following:

False
False
False
Tags What's this?: Add a tag
Flag as ContentBug
Assembly Version - using PowerShell      Thomas Lee   |   Edit   |   Show History
# Get-AssemblyVersion.ps1
# Sample using PowerShell
# Thomas Lee - tfl@psp.co.uk

# Define the assembly we want to load - a random reference assembly from SDK 3.0
$Pshfile = "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\system.speech.dll"

# Now load the assembly
$Myasm = [System.Reflection.Assembly]::Loadfile($Pshfile)

# Get name, version and display the results
$Aname = $Myasm.GetName()
$Aver = $Aname.version

# display results
"Assembly: {0} has version number of: {1}" -f $Aname.name, $aver

This assembly produces the following output:
PS C:\foo> .\get-assemblyversion.ps1
Assembly: System.Speech has version number of: 3.0.0.0

Automatic Build and Revision numbers      John Leidegren   |   Edit   |   Show History

When specifying a version, you have to at least specify major. If you specify major and minor, you can specify an asterisk (*) for build. This will cause build to be equal to the number of days since January 1, 2000 local time, and for revision to be equal to the number of seconds since midnight local time, divided by 2.

e.g. AssemblyInfo.cs

[assembly: AssemblyVersion("1.0.*")] // important: use wildcard for build and revision numbers!

SampleCode.cs

var version = Assembly.GetEntryAssembly().GetName().Version;
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(
TimeSpan.TicksPerDay * version.Build + // days since 1 January 2000
TimeSpan.TicksPerSecond * 2 * version.Revision))); // seconds since midnight, (multiply by 2 to get original)
Flag as ContentBug
Assembly is in the System.Reflection NameSpace      Todd Beaulieu   |   Edit   |   Show History

What's not immediately obvious in these examples is that, while Version is in the System NameSpace, Assembly is in the System.Reflection NameSpace.

Oftentime, examples fail to mention the needed references (using statements), which can result in wasted time trying to get them to work.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
Page view tracker