How to Auto Increment the Version (e.g., “1.0.*”) of a .NET Core Project?
Auto-incrementing the version of a .NET Core project can help you easily track and manage your application's releases. Here's a step-by-step guide to achieve this:
- Edit the .csproj File:
<PropertyGroup> <GenerateAssemblyInfo>false</GenerateAssemblyInfo> <Deterministic>false</Deterministic> </PropertyGroup>
- Add AssemblyVersion Attribute:
In your code, typically in the entry point class, add the following attribute:
using System.Reflection; [assembly: AssemblyVersion("1.0.*")]
- Customize Versioning:
For more customization options, refer to this article:
https://sachabarbs.wordpress.com/2020/02/23/net-core-standard-auto-incrementing-versioning/ - Use Visual Studio Extension:
For Visual Studio users, consider using the "Automatic Versions" extension:
https://marketplace.visualstudio.com/items?itemName=PrecisionInfinity.AutomaticVersions
Additional Techniques:
- Suppress FileVersion Attribute Generation:
This allows you to auto-fill the file version with the generated assembly version. (Credits: this answer.)<PropertyGroup> <AssemblyVersion>1.0.*</AssemblyVersion> <GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute> <Deterministic>false</Deterministic> </PropertyGroup>
- Generate Build Number:
Use the following code to generate the build number in the format "Year.Month.Day.incremental":
<VersionSuffix>$([System.DateTime]::UtcNow.ToString(yyyy.MM.dd.mmff))</VersionSuffix> <AssemblyVersion Condition=" '$(VersionSuffix)' == '' ">0.0.0.1</AssemblyVersion> <AssemblyVersion Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)</AssemblyVersion> <Version Condition=" '$(VersionSuffix)' == '' ">0.0.1.0</Version> <Version Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)</Version> <GenerateAssemblyFileVersionAttribute >false</GenerateAssemblyFileVersionAttribute > <GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GenerateAssemblyInfo>true</GenerateAssemblyInfo> <Deterministic>false</Deterministic> <GenerateAssemblyTitleAttribute>true</GenerateAssemblyTitleAttribute> <GenerateAssemblyConfigurationAttribute>true</GenerateAssemblyConfigurationAttribute> <GenerateAssemblyCompanyAttribute>true</GenerateAssemblyCompanyAttribute> <GenerateAssemblyProductAttribute>true</GenerateAssemblyProductAttribute> <GenerateAssemblyCopyrightAttribute>true</GenerateAssemblyCopyrightAttribute> <GenerateAssemblyVersionAttribute>true</GenerateAssemblyVersionAttribute> <GenerateAssemblyInformationalVersionAttribute>true</GenerateAssemblyInformationalVersionAttribute>
By following these methods, you can easily auto-increment the version of your .NET Core project and streamline your release management process.