Recipe 2.5. Getting an Application s Version Number

Recipe 2.5. Getting an Application's Version Number


You would like to display the version number of your application on its "About" form.


Sample code folder: Chapter 02\VersionNumbers

Use the My.Application.Info.Version object to access the version number of the application, and store the result in a Label control.


Visual Basic stores an application's version number as a four-part "dot"-delimited value, such as: 

The four components represent the major, minor, build, and revision numbers, respectively. They are made available through an instance of the System.Version class obtained from the My.Application.Info.Version object. You can use the members of this class to display version information when needed. The following code assumes your form has a label named VersionNumber:

 Public Class Form1    Private Sub Form1_Load(ByVal sender As System.Object, _          ByVal e As System.EventArgs) Handles MyBase.Load       With My.Application.Info.Version          VersionNumber.Text = "Version " & .Major & _             "." & .Minor & " (Build " & .Build & "." & _             .Revision & ")"       End With    End Sub End Class 

Figure 2-4 displays the typical output for a version value set to

Figure 2-4. Displaying an application version number

If you aren't concerned about the display format of the version number, have the Version object format itself:

 VersionNumber.Text = My.Application.Info.Version.ToString() 

Each .NET assembly has a four-part version number, defined as an assembly attribute in the project's source code. In a typical Visual Basic 2005 application, this attribute is stored in the AssemblyInfo.vb file, which appears only when you have Show All Files enabled in Visual Studio's Solution Explorer panel. If you open this file, you will quickly find the line that sets the version number:

 <Assembly: AssemblyVersion("")> 

Altering the four-part number in the string modifies the assembly's version number. Visual Studio also provides a way to set this through a property form. From the Project Properties window, select the Application tab, and then click the Assembly Information button. The version number is set through the four fields named Assembly Version.

See Also

Recipe 2.6 adds some automation to the version number process.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: