我希望每次构建都增加我的应用程序的版本属性,但我不确定如何在 Visual Studio (2005/2008) 中启用此功能。我试图将 AssemblyVersion 指定为 1.0.* 但它并没有得到我想要的。 我还使用了一个设置文件,在早期的尝试中,当程序集版本更改时,我的设置被重置为默认值,因为应用程序在另一个目录中查找设置文件。 我希望能够以 1.1.38 的形式显示版本号,这样当用户发现问题时,我可以记录他们正在使用的版本,并告诉他们如果有旧版本则升级。 对版本控制如何工作的简短解释也将不胜感激。构建和修订号何时增加?


对于“内置”的东西,你不能,因为使用 1.0.* 或 1.0.0.* 将用编码的日期/时间戳替换修订版和内部版本号,这通常也是一个好方法。 有关详细信息,请参阅/v 标记中的程序集链接器文档。 至于自动递增数字,请使用 AssemblyInfo 任务: 装配信息任务 This can be configured to automatically increment the build number. There are 2 Gotchas: Each of the 4 numbers in the Version string is limited to 65535. This is a Windows Limitation and unlikely to get fixed. Why are build numbers limited to 65535? Using with with Subversion requires a small change: Using MSBuild to generate assembly version info at build time (including SubVersion fix) Retrieving the Version number is then quite easy: Version v = Assembly.GetExecutingAssembly().GetName().Version; string About = string.Format(CultureInfo.InvariantCulture, @"YourApp Version {0}.{1}.{2} (r{3})", v.Major, v.Minor, v.Build, v.Revision); And, to clarify: In .net or at least in C#, the build is actually the THIRD number, not the fourth one as some people (for example Delphi Developers who are used to Major.Minor.Release.Build) might expect. In .net, it's Major.Minor.Build.Revision.
VS.NET defaults the Assembly version to 1.0.* and uses the following logic when auto-incrementing: it sets the build part to the number of days since January 1st, 2000, and sets the revision part to the number of seconds since midnight, local time, divided by two. See this MSDN article. Assembly version is located in an assemblyinfo.vb or assemblyinfo.cs file. From the file: ' Version information for an assembly consists of the following four values: ' ' Major Version ' Minor Version ' Build Number ' Revision ' ' You can specify all the values or you can default the Build and Revision Numbers ' by using the '*' as shown below: '
I have found that it works well to simply display the date of the last build using the following wherever a product version is needed: System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString("yyyy.MM.dd.HH.mm.ss") Rather than attempting to get the version from something like the following: System.Reflection.Assembly assembly=System.Reflection.Assembly.GetExecutingAssembly(); object[] attributes=assembly.GetCustomAttributes(typeof(System.Reflection.AssemblyFileVersionAttribute), false); object attribute=null; if (attributes.Length > 0) { attribute = attributes[0] as System.Reflection.AssemblyFileVersionAttribute; }
[Visual Studio 2017, .csproj properties] To automatically update your PackageVersion/Version/AssemblyVersion property (or any other property), first, create a new Microsoft.Build.Utilities.Task class that will get your current build number and send back the updated number (I recommend to create a separate project just for that class). I manually update the major.minor numbers, but let MSBuild to automatically update the build number (1.1.1, 1.1.2, 1.1.3, etc. :) using Microsoft.Build.Framework; using System; using System.Collections.Generic; using System.Text; public class RefreshVersion : Microsoft.Build.Utilities.Task { [Output] public string NewVersionString { get; set; } public string CurrentVersionString { get; set; } public override bool Execute() { Version currentVersion = new Version(CurrentVersionString ?? "1.0.0"); DateTime d = DateTime.Now; NewVersionString = new Version(currentVersion.Major, currentVersion.Minor, currentVersion.Build+1).ToString(); return true; } } Then call your recently created Task on MSBuild process adding the next code on your .csproj file: ... ... .. 1.1.4 .. When picking Visual Studio Pack project option (just change to BeforeTargets="Build" for executing the task before Build) the RefreshVersion code will be triggered to calculate the new version number, and XmlPoke task will update your .csproj property accordingly (yes, it will modify the file). When working with NuGet libraries, I also send the package to NuGet repository by just adding the next build task to the previous example. c:\nuget\nuget is where I have the NuGet client (remember to save your NuGet API key by calling nuget SetApiKey or to include the key on the NuGet push call). Just in case it helps someone ^_^.
What source control system are you using? Almost all of them have some form of $ Id $ tag that gets expanded when the file is checked in. I usually use some form of hackery to display this as the version number. The other alternative is use to use the date as the build number: 080803-1448
Some time ago I wrote a quick and dirty exe that would update the version #'s in an assemblyinfo.{cs/vb} - I also have used rxfind.exe (a simple and powerful regex-based search replace tool) to do the update from a command line as part of the build process. A couple of other helpfule hints: separate the assemblyinfo into product parts (company name, version, etc.) and assembly specific parts (assembly name etc.). See here Also - i use subversion, so I found it helpful to set the build number to subversion revision number thereby making it really easy to always get back to the codebase that generated the assembly (e.g. 1.4.100.1502 was built from revision 1502).
If you want an auto incrementing number that updates each time a compilation is done, you can use VersionUpdater from a pre-build event. Your pre-build event can check the build configuration if you prefer so that the version number will only increment for a Release build (for example).
Here is a handcranked alternative option: This is a quick-and-dirty PowerShell snippet I wrote that gets called from a pre-build step on our Jenkins build system. It sets the last digit of the AssemblyVersion and AssemblyFileVersion to the value of the BUILD_NUMBER environment variable which is automatically set by the build system. if (Test-Path env:BUILD_NUMBER) { Write-Host "Updating AssemblyVersion to $env:BUILD_NUMBER" # Get the AssemblyInfo.cs $assemblyInfo = Get-Content -Path .\MyShinyApplication\Properties\AssemblyInfo.cs # Replace last digit of AssemblyVersion $assemblyInfo = $assemblyInfo -replace "^\[assembly: AssemblyVersion\(`"([0-9]+)\.([0-9]+)\.([0-9]+)\.[0-9]+`"\)]", ('[assembly: AssemblyVersion("$1.$2.$3.' + $env:BUILD_NUMBER + '")]') Write-Host ($assemblyInfo -match '^\[assembly: AssemblyVersion') # Replace last digit of AssemblyFileVersion $assemblyInfo = $assemblyInfo -replace "^\[assembly: AssemblyFileVersion\(`"([0-9]+)\.([0-9]+)\.([0-9]+)\.[0-9]+`"\)]", ('[assembly: AssemblyFileVersion("$1.$2.$3.' + $env:BUILD_NUMBER + '")]') Write-Host ($assemblyInfo -match '^\[assembly: AssemblyFileVersion') $assemblyInfo | Set-Content -Path .\MyShinyApplication\Properties\AssemblyInfo.cs -Encoding UTF8 } else { Write-Warning "BUILD_NUMBER is not set." }