我只是想知道如何使用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)
要获得版本号,请尝试
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Reflection.AssemblyName assemblyName = assembly.GetName();
Version version = assemblyName.Version;
要设置版本号,请创建/编辑AssemblyInfo.cs
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.*")]
另外,作为旁注,第三个数字是自2000年2月1日以来的天数,第四个数字是当天总秒数的一半。所以如果你在午夜编译,它应该是0。
Cake支持对AssemblyInfo文件进行补丁。有了蛋糕在手,你有无限的方法来实现自动版本增量。
像c#编译器一样增加版本的简单例子:
Setup(() =>
{
// Executed BEFORE the first task.
var datetimeNow = DateTime.Now;
var daysPart = (datetimeNow - new DateTime(2000, 1, 1)).Days;
var secondsPart = (long)datetimeNow.TimeOfDay.TotalSeconds/2;
var assemblyInfo = new AssemblyInfoSettings
{
Version = "3.0.0.0",
FileVersion = string.Format("3.0.{0}.{1}", daysPart, secondsPart)
};
CreateAssemblyInfo("MyProject/Properties/AssemblyInfo.cs", assemblyInfo);
});
在这里:
Version -是汇编版本。最佳实践是锁定主要版本号并保留零(如“1.0.0.0”)。
FileVersion -是程序集文件版本。
请注意,您不仅可以修补版本,还可以修补所有其他必要的信息。
在每次构建中更改版本号的另一个选项是使用MSBuild.Community.Tasks的version任务。只需下载他们的安装程序,安装它,然后调整以下代码,并将其粘贴到<Import Project="$(MSBuildBinPath)\Microsoft.CSharp。在你的。csproj文件中的Targets " />:
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<Target Name="BeforeBuild">
<Version VersionFile="Properties\version.txt" Major="1" Minor="0" BuildType="Automatic" StartDate="12/31/2009" RevisionType="BuildIncrement">
<Output TaskParameter="Major" PropertyName="Major" />
<Output TaskParameter="Minor" PropertyName="Minor" />
<Output TaskParameter="Build" PropertyName="Build" />
<Output TaskParameter="Revision" PropertyName="Revision" />
</Version>
<AssemblyInfo CodeLanguage="CS"
OutputFile="Properties\VersionInfo.cs"
AssemblyVersion="$(Major).$(Minor)"
AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" />
</Target>
注意:根据您的语言环境调整StartDate属性。它目前不使用不变区域性。
对于2010年1月14日的第三个版本,这将创建一个包含以下内容的VersionInfo.cs:
[assembly: AssemblyVersion("1.0")]
[assembly: AssemblyFileVersion("1.0.14.2")]
然后必须将该文件添加到项目中(通过添加现有项),并且必须从AssemblyInfo.cs中删除AssemblyVersion和AssemblyFileVersion行。
用于更改版本组件的不同算法在$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks中描述。chm和版本属性。
如何获取版本{major}.{year}.1{date}.1{time}
这个有点实验性,但我喜欢。灵感来自Jeff Atwood @ CodingHorror(链接)。
结果的版本号变成1.2016.10709.11641(意思是2016-07-09 16:41),这允许
可怜的男人零填充(愚蠢的前导1)
嵌入到版本号中的近乎人类可读的本地DateTime
对于真正重大的突破性更改,不要使用Major版本。
在项目中添加一个新项,选择General -> Text Template,将其命名为CustomVersionNumber,并(如果适用)在Properties/AssemblyInfo.cs中注释出AssemblyVersion和AssemblyFileVersion。
然后,在保存该文件或构建项目时,这将重新生成一个.cs文件,该文件位于已创建的.tt文件下的子项。
<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
//
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.
//
using System.Reflection;
<#
var date = DateTime.Now;
int major = 1;
int minor = date.Year;
int build = 10000 + int.Parse(date.ToString("MMdd"));
int revision = 10000 + int.Parse(date.ToString("HHmm"));
#>
[assembly: AssemblyVersion("<#= $"{major}.{minor}.{build}.{revision}" #>")]
[assembly: AssemblyFileVersion("<#= $"{major}.{minor}.{build}.{revision}" #>")]
在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