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.
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}
- 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:
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace Sample_Version_App
{
- In the event, include the following code:
private void Form1_Load(object sender, EventArgs e)
{
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.
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