我只是想知道如何使用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)
在每次构建中更改版本号的另一个选项是使用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和版本属性。
将递增(DateTime)信息放入AssemblyFileVersion属性,该属性的优点是不破坏任何依赖关系。
基于Boog的解决方案(不适合我,也许是因为VS2008?),你可以结合使用一个预构建事件生成一个文件,添加该文件(包括其版本属性),然后使用一种方法再次读取这些值。这是. .
Pre-Build-Event:
echo [assembly:System.Reflection.AssemblyFileVersion("%date:~-4,4%.%date:~-7,2%%date:~-10,2%.%time:~0,2%%time:~3,2%.%time:~-5,2%")] > $(ProjectDir)Properties\VersionInfo.cs
将生成的VersionInfo.cs文件(Properties子文件夹)包含到项目中
返回日期(年到秒)的代码:
var version = assembly.GetName().Version;
var fileVersionString = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;
Version fileVersion = new Version(fileVersionString);
var buildDateTime = new DateTime(fileVersion.Major, fileVersion.Minor/100, fileVersion.Minor%100, fileVersion.Build/100, fileVersion.Build%100, fileVersion.Revision);
不太舒服…此外,我不知道它是否会创建大量的强制重建(因为文件总是在变化)。
例如,如果每隔几分钟/小时才更新VersionInfo.cs文件(通过使用临时文件,然后在检测到足够大的更改时复制/覆盖真正的VersionInfo.cs),您可以使其更加智能。我曾经非常成功地做到过。
对于任何使用Tortoise Subversion的人,您可以将您的版本号之一绑定到源代码的Subversion修订号。我发现这非常有用(审计人员也非常喜欢!)。您可以通过在预构建中调用WCREV实用程序并从模板生成AssemblyInfo.cs来实现这一点。
如果您的模板名为AssemblyInfo。wcrev位于正常的AssemblyInfo.cs目录中,而tortoise位于默认安装目录中,那么你的预构建命令看起来像这样(注意,都在一行中):
"C:\Program Files\TortoiseSVN\bin\SubWCRev.exe" "$(ProjectDir)." "$(ProjectDir)Properties\AssemblyInfo.wcrev" "$(ProjectDir)Properties\AssemblyInfo.cs"
模板文件将包含wcrev令牌替换字符串:$ wcrev $
如。
[assembly: AssemblyFileVersion("1.0.0.$WCREV$")]
注意:
由于现在生成了AssemblyInfo.cs,您不希望对其进行版本控制。