Coder Perfect

How can I have a version number that automatically increments in Visual Studio? [duplicate]


I’d like to keep track of a collection of integers that are automatically incremented at build time:

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

It would auto-increment Revision when I compile. MinorVersion would be incremented when I built the setup project (I’m fine with doing this manually). MajorVersion would only be manually incremented.

Then I could show the user a version number in the Help/About menu, such as:

  Version: 0.1.92

What is the best way to accomplish this?

This question not only asks how to create an auto-incrementing version number, but also how to use it in code, making it a more comprehensive answer than others.

Asked by esac

Solution #1

If you add an AssemblyInfo class to your project and change the AssemblyVersion attribute to finish with an asterisk, your project will look like this:

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

Visual studio will automatically increment the final number based on these guidelines (thanks galets, I was absolutely incorrect!)

You utilize reflection to refer to this version in code so you may show it to the user. As an example,

Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
DateTime buildDate = new DateTime(2000, 1, 1)
                        .AddDays(version.Build).AddSeconds(version.Revision * 2);
string displayableVersion = $"{version} ({buildDate})";

It’s also worth noting that if you include both AssemblyVersion and AssemblyFileVersion, you won’t see this on your.exe.

It’s not usually a good idea to set the 4th number to *, because the version won’t always increase. The number of days since the year 2000 is the third number, and the number of seconds since midnight is the fourth number (divided by 2) [IT DOESN’T HAPPEN AT RANDOM]. As a result, if you created the solution late one day and early the next, the later build will have a lower version number. Because your version number will ALWAYS rise if you use X.Y.* instead of X.Y.Z.*, I advocate doing so.

Answered by Noel Kennedy

Solution #2

To generate the appropriate source code from a simple text file, you might utilize Visual Studio’s T4 templating mechanism:

<#@ template language="C#" #>
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.

using System.Reflection;

[assembly: AssemblyVersion(“1.0.1.<#= this.RevisionNumber #>”)]

[assembly: AssemblyFileVersion(“1.0.1.<#= this.RevisionNumber #>”)]

<#+ int RevisionNumber = (int)(DateTime.UtcNow – new DateTime(2010,1,1)).TotalDays; #>

// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.

using System.Reflection;

[assembly: AssemblyVersion(“”)]

[assembly: AssemblyFileVersion(“”)]

Answered by 5 revs, 5 users 80%

Solution #3

This is how I implemented T4’s proposal… This will increment the build number independent of the option (Debug|Release), and it will increment the revision number every time you conduct a Release build. Through Application Assembly Information, you can continue to update the major and minor version numbers…

To clarify, this will read the existing AssemblyInfo.cs file, use regex to find the AssemblyVersion information, and then increment the revision and build numbers based on TextTransform.exe input.

Answered by Drew Chapin

Solution #4

If you enter an asterisk for build and revision, Visual Studio calculates the build number as the number of days since January 1, 2000, and the revision number as the number of seconds since midnight divided by two. is a MUCH better life saver option.

It works perfectly and is really adaptable.

Answered by gideon

Solution #5

The following is a quote from MSDN about AssemblyInfo.cs:

This effectively indicates that if you put a 1.1.* in the assembly information, only the build number will autoincrement, and it will happen daily, not after each build. Every build will have a different revision number, but it will vary at random rather than incrementing.

For the most part, this should suffice. If that’s not what you’re looking for, you’ll have to construct a script to autoincrement the version number on the pre-build phase.

Answered by galets

Post is based on