Export (0) Print
Expand All
7 out of 11 rated this helpful - Rate this topic

Version Class

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class Version : ICloneable, 
	IComparable, IComparable<Version>, IEquatable<Version>

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.

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.

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

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

    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());
       }
    }
    
  • 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.

    using System;
    using System.Reflection;
    
    public class Example
    {
       public static void Main()
       {
          // Get the version of the current assembly.
          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.

    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());
       }
    

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.

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.

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.

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

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. At run time, the example uses the Assembly.GetExecutingAssembly method to get a reference to the executable 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 = Assembly.GetExecutingAssembly();
      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.
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

Added information about comparing Version objects.

Information enhancement.

July 2008

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

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.