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.
|
' 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())
|
|
// 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.
Dim ver As Version = Environment.Version
Console.WriteLine("CLR Version {0}", ver.ToString())
|
|
// 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.
|
' 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())
|
|
// 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.
|
' 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())
|
|
// 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.
|
' 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())
|
|
// 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());
|