dotnet build MyProject.csproj --configuration Release /p:Version=%1
# 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
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"
The environment variables $Env:BUILD_SOURCESDIRECTORY
and $Env:BUILD_BUILDNUMBER
are both provided by the build process as part of the Microsoft build environment.
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))
# 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)
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>1.0.1.87</Version>
<AssemblyVersion>1.0.1.87</AssemblyVersion>