How to auto increment version number of assembly in .NET and Visual Studio using C#
In .NET, version information for an assembly follows following format - [*.*.*.*]
.
This version format consist of four values:
- Major Version
- Minor Version
- Build Number
- Revision
Above four value can be used to indicate different things depending on how you want them to be understood.
When we compile or build our code in .NET (using Visual studio), it outputs assemblies and this assembly can be tagged with three different types of versions, which are as follows:
- Assembly Version
- Assembly File Version
- Assembly Informational Version
Now let’s take a minute to understand what are these and how or in which context are they used.
What is Assembly Version?
I call this version as the pure .NET version, but to be specific it is important in your runtime scope. When any application or other assembly references this assembly, this is the version number they will try to find and remember the referenced assembly by this version number. So when this version changes the assembly that is references it will update their reference for this assembly.
Some information from MSDN:
The default version policy for the runtime is that applications run only with the versions they were built and tested with, unless overridden by explicit version policy in configuration files (the application configuration file, the publisher policy file, and the computer's administrator configuration file)”
Version checking only occurs with strong-named assemblies
Q - Is AssemblyVersion attribute mandatory?
A – Yes and no. Yes it is required. But if you remove the AssemblyVersion attribute from AssemblyInfo.cs file, compiler will add this attribute with default value as [0.0.0.0].
What is Assembly File Version?
This represents the actual file version, this can be used to differentiate between two copies of same assemblies from different builds. It is the version number for Win32 file version resource.
You can view the assembly file version in windows explorer by opening file property for the assembly in details tab – “File Version” attribute.
Q – Is AssemblyFileVersion attribute mandatory?
A – No, if you do not specify AssemblyFileVersion attribute, it uses value from AssemblyVersion attribute.
What is Assembly Information Version?
This is an additional version number which can be used as friendly way to display your version to end users or can contain additional version information for an assembly.
Q – Is AssemblyInformationVersion mandatory?
A – No, if you do not specify AssemblyInformationVersion
attribute, it will use the value from AssemblyFileVersion attribute.
Version numbering:
- All part of the version must be integers and should be greater than or equal to 0.
AssemblyVersion | AssemblyFileVersion | AssemblyInformationVersion |
---|---|---|
Yes | Yes | N/A |
AssemblyVersion | AssemblyFileVersion | AssemblyInformationVersion |
---|---|---|
Yes | Yes | N/A |
Few sample version numbers:
Sample Version Number | Assembly Version | Assembly File Version | Assembly Information Version |
---|---|---|---|
[1.0.0.0] | Allowed | Allowed | Allowed |
[1] | Allowed | Allowed | Allowed |
[1.2] | Allowed | Allowed | Allowed |
[1.2.3] | Allowed | Allowed | Allowed |
[0.0.0.0] | Allowed | Allowed | Allowed |
[1.2.RC1] | Not Allowed | Not Allowed | Allowed |
[1.] | Error | Error | Allowed |
[1.0.] | Error | Error | Allowed |
[1.2.*] | Allowed | Allowed | Allowed |
[1.2.*.*] | Allowed | Allowed | Allowed |
[1.2.*.23] | Not Allowed | Not Allowed | Allowed |
In above list you see in the last three samples, I have specified *
mark, for AssemblyVersion
and AssemblyFileVersion
this indicates that Build Number and Revision will be generated by compiler while building code.
One * after the Major and Minor number indicates that Build number and Revision will be generated automatically by compiler. If you specify * in Build number part and specify number for Revision then compiler will provide error for the same.
For AssemblyInformationVersion attribute considers all value as string so all the above samples are allowed (testing in VS2012).
According to MSDN about auto increment version number:
The default build number increments daily. The default revision number is random.
Question is – How does compiler generate and assign default version number for Build number and Revision? Because above information does not specify anything specific.
Build Number component – Number of days since the year 2000.
Revision component – Number of seconds since midnight divided by 2 and yes this number is not random as stated by MSDN.
Here you can see the version number of the executing assembly and the calculated value for number of days since the year 2000 and number of seconds since midnight divided by 2.
Due to this reason, it is not recommended to specify version number as [1.2.3.*] because the Revision number may be same for two assemblies as it will use the same numbers every day based on calculation for number of seconds since midnight and this may result into two assembly with same version number.
Sample code on Github for How to auto increment version number of assembly in .NET and Visual Studio using C#