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

How to set your App's Version Number to auto-increment the Build and Revision portions and Display the Version in a Label

Rate me:
Please Sign up or sign in to vote.
4.61/5 (13 votes)
22 Dec 2014CPOL3 min read 52.5K   19   6
Setting an assembly's version number to a "starting point" but allowing Visual Studio to auto-increment the Build and Revision elements of the Version Info, and then display that in an About box

Version Birth / Immaculate Incrementation

It seems everybody wants to call the next version of their app "vNext", or "2.0" (but not "9").

That's fine, it's your app, and you can name it and version it however you wish. To keep track of which specific version (not just the major and perhaps minor portions) a member of your team or a customer is currently using, though, you probably want something more specific - in addition to major and minor, you probably also want the build and revision numbers.

This is easy to set up and make available in a C# Visual Studio app. Here's how:

From within your project, open Properties > AssemblyInfo. You should see something like this:

C#
[assembly: AssemblyVersion("1.0.0.0")] 

To set the major and minor versions, just directly edit those first two values. Then, replace the trailing "0.0" with a star. For example, if the version you're working on is 2.0, you could do it like so:

C#
[assembly: AssemblyVersion("2.0.*")]

Now your build and revision values will be automatically updated. Each build of your app will be distinguishable from all others builds. However, this won't do you a heck of a lot of good if you don't provide an easy way for a user to check the version value. This is easy, too. In a form (such as an "About box") put code like this in the form's constructor:

C#
public frmAbout()
{
    InitializeComponent();
    Version versionInfo = Assembly.GetExecutingAssembly().GetName().Version;
    // Concise way to do this suggested by james_carter:
    lblVersion.Text = "Version " + versionInfo.ToString()
}

The code above assumes the form's name is "frmAbout" and it has a label named lblVersion.

Now you will only need to update version information when you go to version 2.1, or 2.5, or 3.0, or whatever you decide to use as the next version number. You could even use "Microsoft math/logic" and skip version numbers if you want to.

When a Build is not a Build

Sometimes a kiss is just a kiss, but wackily enough, sometimes a build is not really a build at all. What I'm trying to break to you ever so gently is that the Build number does not really increment with each build; rather, it increments with each day that passes. And the starting point for this rather unusual perspective of what a build is is 1/1/200. IMO, this VersionInfo member should be named something other than "Build", this being the case.

After all, to quote the late great Abraham Lincoln, calling a tail a leg doesn't make the tail a leg; and similarly, calling an arbitrarily-arrived-at value a Build number does not make it so or such.

Nevertheless, to paraphrase Vince Lombardi, when the going gets weird, the weird get going...?!? Anyway, knowing the madness behind this method, we can compute the date of the last build and display that to the user, like so:

C#
DateTime startDate = new DateTime(2000, 1, 1);
int diffDays = versionInfo.Build;
DateTime computedDate = startDate.AddDays(diffDays);
lblLastBuilt.Text += computedDate.ToLongDateString();

Provided you have added a label to your About box, given it a design-time text value of "Last built on " , and named it "lblLastBuilt" the code above should "just work". You will see the label sport a proclamation such as: "Last built on Thursday, December 18, 2014".

Shameless Supplication

I'm still (somewhat impatiently) waiting for my Choctypus!

Note: A Choctypus, contrary to unpopular (with me) opinion, has decidedly nothing whatsoever to do with Octopi nor with Cheetahs, rather it is (what should have been obvious, methinks), a CHOColate duckbilled plaTYPUS!

License

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


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
QuestionThis is the code I use... Pin
Alexms23-Dec-14 10:40
Alexms23-Dec-14 10:40 
Questionre: Assembly File Version Warning Pin
james_carter23-Dec-14 3:34
james_carter23-Dec-14 3:34 
QuestionVS emits a warning unless AssemblyFileVersion is commented out Pin
PaperTape22-Dec-14 22:14
professionalPaperTape22-Dec-14 22:14 
QuestionOddly enjoyable Pin
Chris Maunder22-Dec-14 16:10
cofounderChris Maunder22-Dec-14 16:10 
QuestionBuild or not build Pin
TheComputerMan20-Dec-14 0:42
TheComputerMan20-Dec-14 0:42 
QuestionversionInfo.ToString() Pin
james_carter19-Dec-14 2:44
james_carter19-Dec-14 2:44 

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.