views:

7245

answers:

7

I want to store a set of integers that get auto incremented at build time:

int MajorVersion = 0;
int MinorVersion = 1;
int Revision = 92;

When I compile, it would auto-increment Revision. When I build the setup project, it would increment MinorVersion (I'm OK with doing this manually). MajorVersion would only be incremented manually.

Then I could display a version number in menu Help/About to the user as:

  Version: 0.1.92

How can this be achieved?

+5  A: 

Use AssemblyInfo.cs

Create the file in App_Code: and fill out the following or use Google for other attribute/property possibilities.

AssemblyInfo.cs

using System.Reflection;

[assembly: AssemblyDescription("Very useful stuff here.")]
[assembly: AssemblyCompany("companyname")]
[assembly: AssemblyCopyright("Copyright © me 2009")]
[assembly: AssemblyProduct("NeatProduct")]
[assembly: AssemblyVersion("1.1.*")]

AssemblyVersion being the part you are really after.

Then if you are working on a website, in any aspx page, or control, you can add in the <Page> tag, the following:

CompilerOptions="<folderpath>\App_Code\AssemblyInfo.cs"

(replacing folderpath with appropriate variable of course).

I don't believe you need to add compiler options in any manner for other classes; all the ones in the App_Code should receive the version information when they are compiled.

Hope that helps.

Micah Smith
+10  A: 

If you add an AssemblyInfo class to your project and ammend the AssemblyVersion attribute to end with an asterisk, for example:

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

Visual studio will increment the final number for you according to these rules (thanks galets, I had that completely wrong!)

To reference this version in code, so you can display it to the user, you use reflection. For example,

string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

I'm not sure what a 'setup project package' is, so can't help with the other requirement.

Noel Kennedy
Thanks Noel. Your comment came in first, and you provided me with the code to retrieve the version as well, so I am giving it to you.
esac
+5  A: 

Here's the quote on AssemblyInfo.cs from MSDN:

You can specify all the values or you can accept the default build number, revision number, or both by using an asterisk (). For example, [assembly:AssemblyVersion("2.3.25.1")] indicates 2 as the major version, 3 as the minor version, 25 as the build number, and 1 as the revision number. A version number such as [assembly:AssemblyVersion("1.2.")] specifies 1 as the major version, 2 as the minor version, and accepts the default build and revision numbers. A version number such as [assembly:AssemblyVersion("1.2.15.*")] specifies 1 as the major version, 2 as the minor version, 15 as the build number, and accepts the default revision number. The default build number increments daily. The default revision number is random

This effectively says, if you put a 1.1.* into assembly info, only build number will autoincrement, and it will happen not after every build, but daily. Revision number will change every build, but randomly, rather than in an incrementing fashion.

This is probably enough for most use cases. If that's not what you're looking for, you're stuck with having to write a script which will autoincrement version # on pre-build step

galets
A: 

You could use the T4 templating mechanism in Visual Studio to generate the required source code from a simple text file. Write template code to read, update and save your text file every time you do a build. Then include the values from the text file in the *.cs file that is the output of the template. You can use partial classes so that your template only has to generate a small part of the class where you want these values to appear. If you need to store your master text file in source control, you have to worry about checking it out each time, which is a hassle, but it's possible.

[warning: I haven't tried this]

(This is a lazy answer. If anyone wants to edit it, please do.)

Robert Lewis
+2  A: 

You could try using UpdateVersion by Matt Griffith. It's quite old now, but works well. To use it, you simply need to setup a pre-build event which points at your AssemblyInfo.cs file, and the application will update the version numbers accordingly, as per the command line arguments.

As the application is open-source, I've also created a version to increment the version number using the format (Major version).(Minor version).([year][dayofyear]).(increment). More information about this and the revised code is available on my blog entry, Assembly Version Numbers and .NET.

Mun
A: 

You can do more advanced versioning using build scripts such as Build Versioning

Raymond
+3  A: 

hi,

If you put an asterisk in for build and revision visual studio uses the number of days since Jan. 1st 2000 as the build number, and the number of seconds since midnight divided by 2 as the revision.

A MUCH better life saver solution is http://autobuildversion.codeplex.com/

It works like a charm and it VERY flexible.

giddy