How to Auto increment version in Visual Studio

By FoxLearn 7/16/2024 7:27:40 AM   116
To use the "Automatic Versions" extension in Visual Studio to automatically increment build numbers, follow these steps.

First, ensure that you have the "Automatic Versions" extension installed in your Visual Studio instance. It's a plugin designed to automatically incrementing Assembly, AssemblyFile, AssemblyInfo, ClickOnce, and package version(s) for C# and VB.NET projects.

Visual Studio 2022 auto increment version

To install Automatic Versions plugin you need to open your Visual Studio, then select Extensions => Manage Extensions... => Install the extension if it's not already installed.

visual studio build numbers

Enter 'Automatic Versions' at the search box.

Next, Find the Automatic Versions extension, then click Download button.

After downloading the file, you need to close the Visual Studio.

 msbuild auto increment version

Click Modify button, then reopen your Visual Studio.

Select Tools => Automatic Versions Settings.

visual studio build numbers

Automatic Versions Settings allow you to configure (Debug/Release) level of a Project, or more generically at the Solution level, or a global user default for all solutions.

Select the project for which you want Visual Studio auto increment.

 c# set assembly version programmatically

For each level that settings can be applied the following options are available.

version type dropdown options

Inherit/Not Set - Each Version type has the option to inherit settings from a parent level, or in the case of the global defaults, Not Set, is used to tell Automatic Versions not to do anything with that version type.

Custom System.Version - Allows incrementation options for the Major.Minor.Build.Revision version format.

Custom Semantic Version (BETA/PRO) - Allows options for Major.Minor.Patch-prerelease version format.

In this example, I'll select the Custom System.Version, then select Build and Revision.

visual studio build numbers

Assembly Version - If set, updates the AssemblyVersion attribute in the AssemblyInfo.cs./vb file. This value is used by .Net to determine which version of a library to load if strong naming is used.

You can get the version number as the following code snippet.

// c# get assembly version
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
Console.WriteLine(version); // -> "1.1.22.11"

Assembly File Version - If set, updates the AssemblyFileVersion attribute in the AssemblyInfo.cs/.vb file. This value is used by the operating system as the "File Version" in the details tab of the dll or exe.

An example of how the file version can be used is to increment it if you apply a patch to your product where the functional underlying APIs are the same for backwards compatibility. You would leave the AssemblyVersion the same, but increment the FileVersion to know which dlls/exes had the patch and which did not.

You can access the file version as the following code snippet.

var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
var fileVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(assemblyLocation).FileVersion; 
Console.WriteLine(fileVersion); // -> "1.2.37.2"

Assembly Info Version (.Net Framework) - If set, updates the AssemblyInformationalVersion attribute in the AssemblyInfo.cs/.vb file. This value is used by the operating system as the "Product version" in the details tab of the dll or exe. This version type is special because it has the option to be used for Semantic Versioning, which can be used for NuGet Publishes. Alternatively, this version attribute can have 'prefix text' that can contain your product name.

To use Prefix text, set the static Prefix text in the AssemblyInfo file and Automatic Versions will ignore it and only update the version numbers. Don't use prefix text with Semantic Versioning because the Semantic Versioning specification doesn't allow it.

You can access the Assembly Informational Version as the following code snippet.

// auto increment version c# example
var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
var version = System.Diagnostics.FileVersionInfo.GetVersionInfo(assemblyLocation).ProductVersion; 
Console.WriteLine(version); // -> "1.2.37-alpha-01"

Package Version - (.Net Standard/Core) If set, updates the Package Version attribute and will automatically be used in the nuget package manifest. The only option for this is Semantic Version. The way to get .net core auto increment version is the same as Assembly Info Version.

ClickOnce Version - If set, updates the ClickOnce Version to match the AssemblyVersion or AssemblyFileVersion.

Custom System.Version Options

Custom System.Version Options

Major, Minor, Build, and Revision can be individually configured to the incrementation type desired:

  1. None - Do not increment this value.
  2. None w/AutoReset - Lets you set the value manually and will not be changed, unless a more significant number increases, in which case it will reset to 0.
  3. Increment (Always) - Always Increments this value by 1*
  4. Increment w/AutoReset - Increments this value by 1, unless a more significant number increases, in which case it will reset to 0.
  5. On Demand (Build New Version) - Increment this value by 1 only when a build is initiated using the Build New Version command (on the context menu of each project). You can set this value on both the Major and the Minor numbers and use the context menu (see usage below) to control which one is incremented.
  6. On Demand w/Reset (Obsolete) - Use Increment w/AutoReset instead. This feature may not be supported in future versions of the product. (Legacy functionality: Increment this value by 1 only when a build is initiated using the Build New Version command (on the context menu of each project). Resets all Increment sub-values.)
  7. Day Of Year (ddd) - Set this value to DateTime.UtcNow.DayOfYear: 211.
  8. Day (dd) - Set this value to the current day of the month: 29.
  9. Month (MM) - Set this value to the current month of the year: 7.
  10. Year (yyyy) - Set this value to the current year: 2024.
  11. Short Year (yy) - Set this value to 2 digit current year: 24.
  12. Date (yyddd) - Set this value to the current date (yyddd format - where ddd is the day of year)**: 24211.
  13. Date (MMdd) - Set this value to the current date (MMdd) where MM is a 2-digit month and dd is a 2-digit day of month: 0729.
  14. UTC Time (HHmm) - Set this value to the current time (HHmm format): 1330.
  15. Delta Days (since 1/1/2000) - Set this value to the number of days that have occurred since January 1, 2000: 8976.
  16. UTC Seconds Since Midnight/2 - If you are using a custom unique time based stamp for your version number, this gives you the most granularity to a single day. The number has to be divided by 2 so it doesn't overflow the max value.

Custom Semantic Version

Semantic Versioning is typically used for 'packaging' the results of a build for use in a manifest file or as a 'published package' of your product. Therefore Semantic Versioning leverages the other version attributes by letting you choose which of those you want to use for your Major/Minor numbers, and independently does it's own incrementation on the Patch and Pre-Release numbers.

Custom Semantic Version

Major/Minor settings - Set to use AssemblyVersion, AssemblyFileVersion, or Set Manually. To set manually, open the AssemblyInfo file (for Full .Net) or the project properties (for .Net Standard/Core) and manually set the major and minor values.

Patch settings - This will increment the Patch number based on the incrementation settings. Increment Once is available for when you switch to a 'pre-release' version of the product, you will typically want the Patch number to Increment Once for the start of your pre-release cycle. After incrementing once, this setting will automatically update to None (whereby Pre-Release will be doing the incrementation).

Pre-Release (optional) - Set to alpha, beta, preview, rc, or N/A (release). If you set a pre-release, then Patch will automatically change to "increment once" for you and the prerelease will increment instead starting with alpha, then alpha-01, alpha-02, etc. When you set this to (N/A release) then Patch will be automatically changed to Increment w/AutoReset for you.

Finally, rebuild your project, then open Assembly Information in project properties to check.

version changer

Create a simple windows forms application, then drag and drop a lable control from the Visual Studio toolbox onto your form designer.

// c# set assembly version programmatically
private void Form3_Load(object sender, EventArgs e)
{
    var version = Assembly.GetExecutingAssembly().GetName().Version;
    lblVersion.Text = version.ToString();
}

Double click on your form, then add a Form_Load event handler to help you show the assembly version.

 visual studio auto increment nuget package version

Location to your .exe file, then right click on your file and select properties. You can see assemblyfileversion auto increment and update in product version.

file properties

Using the Automatic Version plugin you can easily make the Visual Studio auto increment build number version in .NET, .NET Core...