Recipe 2.5. Getting an Application's Version NumberProblemYou would like to display the version number of your application on its "About" form. SolutionSample 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. DiscussionVisual Basic stores an application's version number as a four-part "dot"-delimited value, such as: 1.2.3.4 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 1.2.3.4. Figure 2-4. Displaying an application version numberIf 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("1.0.0.0")> 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 AlsoRecipe 2.6 adds some automation to the version number process. |