Click here to Skip to main content
15,880,392 members
Articles / Programming Languages / C#
Tip/Trick

Automatically display your application version

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
23 Aug 2012CPOL1 min read 52.8K   351   2   4
Automatically display your application version in the UI

Introduction 

I often find myself creating quick utility applications. Because I don't spend a lot of time in the UI it can be difficult down the road to determine what version of the Utility Application someone is using. To help make this a little easier, I have adopted a method of "Stamping" the application version right on the main UI.  Of course you could set this text statically each time you increment your build, but with just a bit of code this can be updated for you automatically.

Sample of Version Label


Creating the Version label

In this section, I will demonstrate how to create the Version label within a C# application.

  • To create this label, simply add a Label to your Windows Form where you would like the version to be displayed.
  • Name the label lblVersion. 
  • Set the Text of the Label to- Version: {0}.{1}.{2}.{3}
 Label added in Vsiual Studio
  • Double click on the form to create the event for the Form_Load method.
  • Note, you will likely need to add the System.Reflection into your using statements section:
C#
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
 
namespace Sample_Version_App
{ 
  • In the event, include the following code:
C#
private void Form1_Load(object sender, EventArgs e)
{
	//set version info
	Version version = Assembly.GetExecutingAssembly().GetName().Version;            
	this.lblVersion.Text = String.Format(this.lblVersion.Text, version.Major, version.Minor, version.Build, version.Revision);
}
  • In this code we get the Version object for the executing application. Using this object we then set the text of the label to show the Major.Minor.Build.Revision of the application.
  • Run the application, note that the version set from the Applications Assembly information is displayed in the form.
Results of the sample application - Shows the app version

Points of Interest

In this project I added the code to dynamically set the version information directly into my main application, but the same concepts could be used to create a User Control that provides the same functionality.


History

  • 08/23/2012
    • Initial publishing of Tip/Trick

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionFor people using ClickOnce... Pin
AndrewB-UK24-Aug-12 2:46
AndrewB-UK24-Aug-12 2:46 
GeneralMy vote of 4 Pin
furill24-Aug-12 2:07
furill24-Aug-12 2:07 
QuestionChanging Version Number Pin
Purushotham Agaraharam24-Aug-12 1:09
Purushotham Agaraharam24-Aug-12 1:09 
AnswerRe: Changing Version Number Pin
tipsybroom24-Aug-12 2:54
tipsybroom24-Aug-12 2:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.