This question already has an answer here:
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?
This question asks not only how to have an auto-incrementing version number, but also how to use that in code which is a more complete answer than others.
Use AssemblyInfo.cs
Create the file in App_Code: and fill out the following or use Google for other attribute/property possibilities.
AssemblyInfo.cs
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:
(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.
If you add an AssemblyInfo class to your project and amend the AssemblyVersion attribute to end with an asterisk, for example:
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,
Two important gotchas that you should know
From @ashes999:
It's also worth noting that if both
AssemblyVersion
andAssemblyFileVersion
are specified, you won't see this on your .exe.From @BrainSlugs83:
Setting only the 4th number to be
*
can be bad, as the version won't always increment. The 3rd number is the number of days since the year 2000, and the 4th number is the number of seconds since midnight (divided by 2) [IT IS NOT RANDOM]. So if you built the solution late in a day one day, and early in a day the next day, the later build would have an earlier version number. I recommend always usingX.Y.*
instead ofX.Y.Z.*
because your version number will ALWAYS increase this way.This is my implementation of the T4 suggestion... This will increment the build number every time you build the project regardless of the selected configuration (i.e. Debug|Release), and it will increment the revision number every time you do a Release build. You can continue to update the major and minor version numbers through Application ➤ Assembly Information...
To explain in more detail, this will read the existing
AssemblyInfo.cs
file, and use regex to find theAssemblyVersion
information and then increment the revision and build numbers based on input fromTextTransform.exe
.AssemblyInfo.cs
file.Create a
AssemblyInfo.tt
file in its place. Visual Studio should createAssemblyInfo.cs
and group it with the T4 file after you save the T4 file.Add this to your pre-build event:
You could use the T4 templating mechanism in Visual Studio to generate the required source code from a simple text file :
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.
Update: I've put the code for my modified version of the UpdateVersion application on GitHub: https://github.com/munr/UpdateVersion
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's VERY flexible.