How to have an auto incrementing version number (Visual Studio)?
If you add an AssemblyInfo class to your project and amend the AssemblyVersion
attribute to end with an asterisk, for example:
[assembly: AssemblyVersion("2.10.*")]
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,
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})";
Three important gotchas that you should know
From @ashes999:
It's also worth noting that if both AssemblyVersion
and AssemblyFileVersion
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 using X.Y.*
instead of X.Y.Z.*
because your version number will ALWAYS increase this way.
Newer versions of Visual Studio give this error:
(this thread begun in 2009)
The specified version string contains wildcards, which are not compatible with determinism. Either remove wildcards from the version string, or disable determinism for this compilation.
See this SO answer which explains how to remove determinism (https://stackoverflow.com/a/58101474/1555612)
Can I automatically increment the file build version when using Visual Studio?
In visual Studio 2008, the following works.
Find the AssemblyInfo.cs file and find these 2 lines:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
You could try changing this to:
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.*")]
But this won't give you the desired result, you will end up with a Product Version of 1.0.* and a File Version of 1.0.0.0. Not what you want!
However, if you remove the second of these lines and just have:
[assembly: AssemblyVersion("1.0.*")]
Then the compiler will set the File Version to be equal to the Product Version and you will get your desired result of an automatically increment product and file version which are in sync. E.g. 1.0.3266.92689
Auto increment FileVersion (build nr) in Visual Studio 2019
Does anyone have a better solution than this ?
Better, I don't know, different yes.
There is no actual need for a 3rd party tool. We use a simple script for doing this and it was running since I can remember (about Visual Studio 2010 I think, with some changes since back then). However, you need to fulfill some prerequisites (but which can come in handy). The script below should run in Visual Studio 2019/2017 and PowerShell 3+.
We have a file version.h
which looks like this:
#pragma once
#define VERSION_MAJOR 1
#define VERSION_MINOR 0
#define VERSION_PATCH 0
#define VERSION_BUILD 0
#define stringify(a) stringify_(a)
#define stringify_(a) #a
Note: version.h requires a new line at the end to avoid fatal error RC1004: unexpected end of file found
.
Resource files can include header files, and therefore include version.h
in your resource file and change it to use the defines accordingly:
#include "version.h"
// ... other stuff
VS_VERSION_INFO VERSIONINFO
FILEVERSION VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_BUILD
PRODUCTVERSION VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_BUILD
// ... other stuff
VALUE "FileVersion", stringify(VERSION_MAJOR) "." stringify(VERSION_MINOR) "." stringify(VERSION_PATCH) "." stringify(VERSION_BUILD)
VALUE "ProductVersion", stringify(VERSION_MAJOR) "." stringify(VERSION_MINOR) "." stringify(VERSION_PATCH) "." stringify(VERSION_BUILD)
// ... other stuff
Now in the project properties pre-build scripts we are running a PowerShell script. The command:
powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File update_version.ps1 "./version.h"
And the script is a find & replace text with an increment:
(Get-Content -path $args[0] -Raw) |
ForEach-Object {
$defstr="#define VERSION_BUILD ";
$regex="$defstr(?<BuildVersion>\d*)";
if($_ -match $regex) {
$_ = $_ -replace $regex,"$($defstr)$(([int]$matches["BuildVersion"])+1)"
}
$_
} |
Out-File $args[0] -encoding ascii -nonewline
Some advantages are:
version.h
is plain C and can be parsed by any C/C++-compliant compiler, MSVC resource compiler or even tools like Doxygen.- You can use it in your C++ files:
#include "version.h"
int main() {
std::cout << "Hello World Version " stringify(VERSION_MAJOR) "." stringify(VERSION_MINOR) "." stringify(VERSION_PATCH) "." stringify(VERSION_BUILD) " !\n";
}
- versioning is centralized
- other scripts to handle major/minor/patch versions are also simple
- the scripts are fairly simple, and maintenance is also simple
Auto-increment version revision number with wildcard
I found this:
https://marketplace.visualstudio.com/items?itemName=PrecisionInfinity.AutomaticVersions
It's hilarious that VS doesn't have this built-in.
How to auto increment the version (eg. “1.0.*”) of a .NET Core project?
One simple way I did it previously is reading the current version and increase it by one, so you get current version and increment by one using command line.
I found this article would answers your question: https://sachabarbs.wordpress.com/2020/02/23/net-core-standard-auto-incrementing-versioning/
How do I auto increment the package version number?
Three liner, versioning by date
I ran into that issue until I figured out after a lot of research how to achieve automatic versioning in just three line in the .csproj
file. Here it is:
<Target Name="NugetPackAutoVersioning" AfterTargets="Build">
<Exec Command="dotnet pack -p:PackageVersion=$([System.DateTime]::Now.ToString("yyyy.MM.dd.HHmm")) --no-build --configuration $(Configuration) --output "$(SolutionDir)nuget"" />
</Target>
This will output a NuGet package named like {ProjectName}.{Year}.{Month}.{Day}.{Hour}{Minute}
in a "nuget" folder at the project root, guaranteeing that packages built later are versioned as posteriors.
Auto Versioning in Visual Studio 2017 (.NET Core)
I have been looking for a version incrementer for a .NET Core app in VS2017 using the csproj configuration format.
I found a project called dotnet bump that worked for the project.json format but struggled to find a solution for the .csproj format. The writer of dotnet bump actually came up with the solution for the .csproj format and it is called MSBump.
There is a project on GitHub for it at:
https://github.com/BalassaMarton/MSBump
where you can see the code and it's available on NuGet too. Just search for MSBump on Nuget.
Related Topics
Why Is There No Foreach Extension Method on Ienumerable
How to Secure an ASP.NET Web API
How to Associate a File Extension to the Current Executable in C#
Ef Core Returns Null Relations Until Direct Access
Line Delimited Json Serializing and De-Serializing
Using C#, How Does One Figure Out What Process Locked a File
C# 3.0 Auto-Properties - Useful or Not
Converting String to Byte Array in C#
C# Getting the Path of %Appdata%
Json.Net Serialize/Deserialize Derived Types
How to Get the Color from a Hexadecimal Color Code Using .Net
Most Efficient Way to Randomly "Sort" (Shuffle) a List of Integers in C#
C# Code to Validate Email Address
Metadataexception: Unable to Load the Specified Metadata Resource