Sunday, September 28, 2008

Build Numbers

Having an automatic versioning for your project is quite useful. Every time you build it, you get a new build number, so you can distinguish between the builds, and make sure your users are always using the latest version (without comparing the exe files).

Today I wanted to make that trick work automatically in C#. That means I have a C# application, which I want to have automatic "Assembly Version". How can this be done in Visual Studio (need to try for mono as well)? Simply. Edit the AssemblyInfo.cs file, and delete the AssemblyFileVersion section. Also, change the AssemblyVersion to something like:

[assembly: AssemblyVersion("1.0.*")]

The * represents the "degree of freedom" for build numbers. That means that the sub-versions after the * (in this case, the third and the fourth numbers) would be automatically incremented by Visual Studio. Actually, it is a number which represents time since January 2000.

Now, all we have to do is to print this value from our code, which usually would be in the "About" dialog. The snippet that gets the version is:

System.Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

Conversion to string can be done with ToString(). That's it.

And in another (related) topic: I became addicted to stackoverflow. I find myself looking for questions which I can answer. Already earned two bronze medals and 76 reputation points.

No comments:

Post a Comment