markneustadt.com

Displaying Assembly Version Information

On a lot of my websites, I find it very useful to know exactly what version I’m working with.  Not only the version number, but also whether or not it’s running in RELEASE or DEBUG mode.

To accomplish this, it’s a fairly simple process.  Just put a Label control on your webform and then set the text property of it.  Here’s some sample code.



Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim strVersion As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()

#If DEBUG Then
    lblVersion.Text = (String.Format("<span align=""center"" style=""width:100%;text-align:center;background-color:red;color:yellow;"">&nbsp;&nbsp;&nbsp;&nbsp;DEBUG MODE - {0}&nbsp;&nbsp;&nbsp;&nbsp;</span>", strVersion))
#Else
    lblVersion.Text = (String.Format("<span align=""center"" style=""width:100%;text-align:center;color:silver;font-size:8pt;"">{0}</span>", strVersion))
#End If

End Sub

The problem I was running into is that the version number was not automatically incrementing.  I did a little searching and found that I should update a file called AssemblyInfo.vb.

A quick look through my solution showed that I didn’t have that file.  I found the area where the data is contained though.  It’s in the project properties.  Specifically on the “Application” tab.

Click the “Assembly Information” button to get the following dialog.

Notice the “Assembly Version” is set to 1.0.0.0.  This is why the version number is fixed.  There’s no option there to set things to auto-increment though.

According to the information I found, I should be able to replace some values with wildcards (asterisks) to get them to automatically increment.  Since I want the Build and the Revision to display the automatic values, I replaced the zeroes with asterisks.

BUZZ…. no dice.  I got the following error message.

It was easy enough to get around this to display the updated revision number.  Just set the build back to a fixed value.

Close… but I don’t want to have to remember to update the build manually everytime I build and deploy.  How to get both the build and the revision to show the automatic values?

A closer look at the error message shows the key.  It’s not very clear but if you put an asterisk in the build field and leave the revision field blank, you’ll get the results you’re after.

Voila!

Incidently, the revision value is “ticks” since midnight of the current day.  The “build” value is the number of days since January 1, 2000.  Using these two values together, you can get a very precise timestamp of your build.

 

UPDATE FOR C#:

Here’s the quick one liner to copy and paste for C#

 string strVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
Scroll To Top