Skip to content

Instantly share code, notes, and snippets.

@DamianSuess
Created March 17, 2025 16:05
Show Gist options
  • Save DamianSuess/df340b86978f8d9c3115df439554fe92 to your computer and use it in GitHub Desktop.
Save DamianSuess/df340b86978f8d9c3115df439554fe92 to your computer and use it in GitHub Desktop.
Build Number Incrementor

Build Incrementor

Build Command

dotnet build MyProject.csproj --configuration Release /p:Version=%1

References

Command Samples

# Build overriding Version Number
dotnet build SolutionName.sln -c Release /property:Version=1.2.3.4

# https://stackoverflow.com/a/52340631/249492
# Publish Sample overriding Assembly and Version number
# - AssemblyVersion=1.2.3.4
# - Version=1.2.3.4-product-version
dotnet publish .\ProjectFolder\Project.csproj -r win-x64 /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true --self-contained true -o ReleaseFolderName -c Release /p:AssemblyVersion=1.2.3.4 /p:Version=1.2.3.4-product-version

Sample 1 - MSBUILD Read CSPROJ and Increment

cls

Write-Host "Versioning started"

"Sources directory " + $Env:BUILD_SOURCESDIRECTORY
"Build number " + $Env:BUILD_BUILDNUMBER

$csprojfilename = $Env:BUILD_SOURCESDIRECTORY+"\MyProject.csproj"
"Project file to update " + $csprojfilename

[xml]$csprojcontents = Get-Content -Path $csprojfilename;
"Current version number is" + $csprojcontents.Project.PropertyGroup.Version

$oldversionNumber = $csprojcontents.Project.PropertyGroup.Version

$csprojcontents.Project.PropertyGroup.Version = $Env:BUILD_BUILDNUMBER
$csprojcontents.Save($csprojfilename)

"Version number has been udated from " + $oldversionNumber + " to " + $Env:BUILD_BUILDNUMBER

Write-Host "Finished"

Notes

The environment variables $Env:BUILD_SOURCESDIRECTORY and $Env:BUILD_BUILDNUMBER are both provided by the build process as part of the Microsoft build environment.

Sample 2

If you encounter an error related to System.Version not taking 5 arguments, enclose the segment dealing with the revision version increment in parentheses as such:

$newVersion = New-Object System.Version($currentVersion.Major, $currentVersion.Minor, $currentVersion.Build, ($currentVersion.Revision + 1))

Sample Code

# Load the assembly containing the Version class
Add-Type -AssemblyName System.Version

# Set the path to the csproj file and load its content
$projectFilePath = "relative/OSNeutral/path/to/your/project.csproj"
$xml = [xml](Get-Content $projectFilePath)

# Get the current version number
$currentVersion = [System.Version]($xml.Project.PropertyGroup.Version)

# Increment the Revision number by 1
$newVersion = New-Object System.Version($currentVersion.Major, $currentVersion.Minor, $currentVersion.Build, $currentVersion.Revision + 1)

# Update the version number in the csproj file
$xml.Project.PropertyGroup.Version = $newVersion.ToString()

# Save the updated csproj file
$xml.Save($projectFilePath)

Sample 2 - CSProj

  <PropertyGroup>
      <TargetFramework>net6.0</TargetFramework>
      <Version>1.0.1.87</Version>

      <AssemblyVersion>1.0.1.87</AssemblyVersion>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment