我接受上面的答案,因为@Gigi目前是正确的,但我很烦恼,于是想出了以下PowerShell脚本。
首先,我在我的解决方案文件夹中有这个脚本(UpdateBuildVersion.ps1):
$path = "$PSScriptRoot\src\ProjectFolder\ProjectName.csproj"
$xml = [xml](Get-Content $path)
$assemblyVersion = $xml.Project.PropertyGroup.AssemblyVersion
$fileVersion = $xml.Project.PropertyGroup.FileVersion
$avMajor, $avMinor, $avBuild = $assemblyVersion.Split(".")
$fvMajor, $fvMinor, $fvBuild = $fileVersion.Split(".")
$avBuild = [Convert]::ToInt32($avBuild,10)+1
$fvBuild = [Convert]::ToInt32($fvBuild,10)+1
$xml.Project.PropertyGroup.AssemblyVersion = "$avMajor.$avMinor.$avBuild"
$xml.Project.PropertyGroup.FileVersion = "$fvMajor.$fvMinor.$fvBuild"
$xml.Save($path)
我将以下代码添加到csproj文件中:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyVersion>0.0.1</AssemblyVersion>
<FileVersion>0.0.1</FileVersion>
<PreBuildEvent>powershell.exe –NonInteractive –ExecutionPolicy Unrestricted -command "& {$(SolutionDir)UpdateBuildVersion.ps1}"</PreBuildEvent>
</PropertyGroup>
</Project>
即使设置为PreBuildEvent,事实上版本号直到文件被加载到内存后才会更新,因此版本号直到下一次构建才会反映出来。实际上,您可以将其更改为PostBuildEvent,其效果是相同的。
我还创建了以下两个脚本:(UpdateMinorVersion.ps1)
$path = "$PSScriptRoot\src\ProjectFolder\ProjectName.csproj"
$xml = [xml](Get-Content $path)
$assemblyVersion = $xml.Project.PropertyGroup.AssemblyVersion
$fileVersion = $xml.Project.PropertyGroup.FileVersion
$avMajor, $avMinor, $avBuild = $assemblyVersion.Split(".")
$fvMajor, $fvMinor, $fvBuild = $fileVersion.Split(".")
$avMinor = [Convert]::ToInt32($avMinor,10)+1
$fvMinor = [Convert]::ToInt32($fvMinor,10)+1
$avBuild = 0
$fvBuild = 0
$xml.Project.PropertyGroup.AssemblyVersion = "$avMajor.$avMinor.$avBuild"
$xml.Project.PropertyGroup.FileVersion = "$fvMajor.$fvMinor.$fvBuild"
$xml.Save($path)
(更新主版本.ps1)
$path = "$PSScriptRoot\src\ProjectFolder\ProjectName.csproj"
$xml = [xml](Get-Content $path)
$assemblyVersion = $xml.Project.PropertyGroup.AssemblyVersion
$fileVersion = $xml.Project.PropertyGroup.FileVersion
$avMajor, $avMinor, $avBuild = $assemblyVersion.Split(".")
$fvMajor, $fvMinor, $fvBuild = $fileVersion.Split(".")
$avMajor = [Convert]::ToInt32($avMajor,10)+1
$fvMajor = [Convert]::ToInt32($fvMajor,10)+1
$avMinor = 0
$fvMinor = 0
$avBuild = 0
$fvBuild = 0
$xml.Project.PropertyGroup.AssemblyVersion = "$avMajor.$avMinor.$avBuild"
$xml.Project.PropertyGroup.FileVersion = "$fvMajor.$fvMinor.$fvBuild"
$xml.Save($path)
/p:
标志给dotnet msbuild
,并设置版本、公司、版权等所有好东西。 - Jay<Deterministic>False</Deterministic>
来使用它(或使用任何其他的MSbuild逻辑来计算<VersionPrefix>
/<Version>
)。 - Martin Ullrich