Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Get Application Version

When I create web apps, I like to have the application version displayed in the footer of each web page so that, as the app. progresses through each environment - dev, acc, prod - I can easily identify which version is being used if a user rings in with an issue.  For some reason, the syntax for getting the assembly version always eludes me; tonight I spent a good 10 minutes trying to remember how to access it.  Here it is, recorded for my own benefit under the category of “Code Snippets”:

    Public ReadOnly Property ApplicationVersion() As String
        Get
            If Application.Item("ApplicationVersion") Is Nothing Then
                Dim ass As [Assembly] = [Assembly].GetExecutingAssembly()
                Dim v As Version = ass.GetName.Version
                Application.Item("ApplicationVersion") = v.ToString
            End If
            Return Application.Item("ApplicationVersion").ToString
        End Get
    End Property

One of the things about the Version class that intrigues me is the, one of the overloads of ToString() takes an integer as an argument which allows you to limit the number of fields that are returned.  Therefore, given a version of 1.1.1.1, calling v.ToString(2) will return “1.1”.  The “gotcha” here is that if the fieldCount is > than the number of components, an argument exception is thrown.  For example, given a version of 1.1.1, calling v.ToString(4) will cause an exception to be thrown.  You'd think that it might have been useful to add some sort of property to Version enabling you to easily find the number of valid fields?

4 Comments

Comments have been disabled for this content.