我只是想知道如何使用Visual Studio(2005)自动增加文件的构建(和版本?)
如果我查找C:\Windows\notepad.exe的属性,版本选项卡给出“文件版本:5.1.2600.2180”。我想在我的dll的版本中也得到这些很酷的数字,而不是版本1.0.0.0,让我们面对它有点沉闷。
我尝试了一些东西,但它似乎没有开箱即用的功能,或者可能我只是在错误的地方(像往常一样)。
我主要工作与网络项目....
我看了两个:
http://www.codeproject.com/KB/dotnet/Auto_Increment_Version.aspx
http://www.codeproject.com/KB/dotnet/build_versioning.aspx
我不敢相信花这么大力气做一件事是标准做法。
编辑:
据我所知,它在VS2005中不工作(http://www.codeproject.com/KB/dotnet/AutoIncrementVersion.aspx)
在Visual Studio 2019中
这对我来说还不够
[assembly: AssemblyVersion("1.0.*")]
在构建时,它抛出这个错误
指定的版本字符串不符合所需的格式
解决方案
在project.csproj中将Deterministic设置为False后,格式最终被接受
<Deterministic>false</Deterministic>
编辑:
出于某种原因,将Deterministic设置为False会使我的配置文件加载并保存在不同的位置。
处理:
我设置了一个构建后事件来增加版本号:
生成后事件批处理脚本
这将调用一个名为autoincrement_version的powershell脚本。ps1将AssemblyInfo.cs的路径作为参数传递
if $(ConfigurationName) == Release (
PowerShell -ExecutionPolicy RemoteSigned $(ProjectDir)autoincrement_version.ps1 '$(ProjectDir)My Project\AssemblyInfo.cs'
)
Poweshell 脚本
它使用Regex自动增加修订号
param( [string]$file );
$regex_revision = '(?<=Version\("(?:\d+\.)+)(\d+)(?="\))'
$found = (Get-Content $file) | Select-String -Pattern $regex_revision
$revision = $found.matches[0].value
$new_revision = [int]$revision + 1
(Get-Content $file) -replace $regex_revision, $new_revision | Set-Content $file -Encoding UTF8
我想出了一个类似于基督徒的解决方案,但不依赖于社区MSBuild任务,这对我来说不是一个选择,因为我不想为我们所有的开发人员安装这些任务。
我正在生成代码并编译到程序集,并想自动增加版本号。但是,我不能使用VS 6.0。* AssemblyVersion技巧,因为它每天自动递增构建号,并破坏与使用旧构建号的程序集的兼容性。相反,我希望有一个硬编码的AssemblyVersion,但要有一个自动递增的AssemblyFileVersion。我通过在AssemblyInfo.cs中指定AssemblyVersion并在MSBuild中生成一个VersionInfo.cs来实现这一点,
<PropertyGroup>
<Year>$([System.DateTime]::Now.ToString("yy"))</Year>
<Month>$([System.DateTime]::Now.ToString("MM"))</Month>
<Date>$([System.DateTime]::Now.ToString("dd"))</Date>
<Time>$([System.DateTime]::Now.ToString("HHmm"))</Time>
<AssemblyFileVersionAttribute>[assembly:System.Reflection.AssemblyFileVersion("$(Year).$(Month).$(Date).$(Time)")]</AssemblyFileVersionAttribute>
</PropertyGroup>
<Target Name="BeforeBuild">
<WriteLinesToFile File="Properties\VersionInfo.cs" Lines="$(AssemblyFileVersionAttribute)" Overwrite="true">
</WriteLinesToFile>
</Target>
这将生成一个VersionInfo.cs文件,该文件具有AssemblyFileVersion的Assembly属性,其中版本遵循带有构建日期的YY.MM.DD.TTTT模式。您必须在项目中包含此文件并使用它进行构建。