C# 自动更新版本号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically update version number
提问by Robert H?glund
I would like the version property of my application to be incremented for each build but I'm not sure on how to enable this functionality in Visual Studio (2005/2008). I have tried to specify the AssemblyVersion as 1.0.* but it doesn't get me exactly what I want.
我希望我的应用程序的版本属性为每个构建增加,但我不确定如何在 Visual Studio (2005/2008) 中启用此功能。我试图将 AssemblyVersion 指定为 1.0.* 但它并没有完全满足我的要求。
I'm also using a settings file and in earlier attempts when the assembly version changed my settings got reset to the default since the application looked for the settings file in another directory.
我还使用了一个设置文件,并且在之前的尝试中,当程序集版本更改时,我的设置被重置为默认值,因为应用程序在另一个目录中查找设置文件。
I would like to be able to display a version number in the form of 1.1.38 so when a user finds a problem I can log the version they are using as well as tell them to upgrade if they have an old release.
我希望能够以 1.1.38 的形式显示版本号,这样当用户发现问题时,我可以记录他们正在使用的版本,并告诉他们如果他们有旧版本则升级。
A short explanation of how the versioning works would also be appreciated. When does the build and revision number get incremented?
对版本控制如何工作的简短解释也将不胜感激。版本号和修订号何时增加?
采纳答案by Michael Stum
With the "Built in" stuff, you can't, as using 1.0.* or 1.0.0.* will replace the revision and build numbers with a coded date/timestamp, which is usually also a good way.
使用“内置”的东西,你不能,因为使用 1.0.* 或 1.0.0.* 将用编码日期/时间戳替换修订和构建数字,这通常也是一种好方法。
For more info, see the Assembly LinkerDocumentation in the /v tag.
有关详细信息,请参阅/v 标记中的程序集链接器文档。
As for automatically incrementing numbers, use the AssemblyInfo Task:
至于自动递增数字,请使用 AssemblyInfo 任务:
This can be configured to automatically increment the build number.
这可以配置为自动增加内部版本号。
There are 2 Gotchas:
有2个陷阱:
- Each of the 4 numbers in the Version string is limited to 65535. This is a Windows Limitation and unlikely to get fixed.
- Using with with Subversion requires a small change:
- 版本字符串中的 4 个数字中的每一个都限制为 65535。这是 Windows 限制,不太可能得到修复。
- 与 Subversion 一起使用需要一个小的改变:
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.
而且,澄清一下:在 .net 或至少在 C# 中,构建实际上是第三个数字,而不是某些人(例如习惯于 Major.Minor.Release.Build 的 Delphi 开发人员)可能期望的第四个数字。
In .net, it's Major.Minor.Build.Revision.
在 .net 中,它是 Major.Minor.Build.Revision。
回答by engtech
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.
几乎所有的文件都有某种形式的 $Id $ 标签,当文件被签入时会被扩展。
I usually use some form of hackery to display this as the version number.
我通常使用某种形式的hackery 将其显示为版本号。
The other alternative is use to use the date as the build number: 080803-1448
另一种选择是使用日期作为内部版本号:080803-1448
回答by caryden
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:
前段时间我写了一个快速而肮脏的 exe,它会更新 assemblyinfo.{cs/vb} 中的版本号 - 我还使用了 rxfind.exe(一个简单而强大的基于正则表达式的搜索替换工具)来执行作为构建过程的一部分,从命令行更新。一些其他有用的提示:
- 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).
- 将装配信息分为产品部分(公司名称、版本等)和装配特定部分(装配名称等)。看这里
- 另外 - 我使用 subversion,所以我发现将内部版本号设置为 subversion 版本号很有帮助,从而可以很容易地始终返回生成程序集的代码库(例如,1.4.100.1502 是从 1502 版本构建的)。
回答by Solracnapod
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.
VS.NET 默认汇编版本为 1.0.* 并在自动递增时使用以下逻辑:它将构建部分设置为自 2000 年 1 月 1 日以来的天数,并将修订部分设置为自午夜以来的秒数,当地时间,除以二。请参阅此MSDN 文章。
Assembly version is located in an assemblyinfo.vb or assemblyinfo.cs file. From the file:
程序集版本位于 assemblyinfo.vb 或 assemblyinfo.cs 文件中。从文件:
' 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:
' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("1.0.0.0")>
<Assembly: AssemblyFileVersion("1.0.0.0")>
回答by user283258
If you want an auto incrementing number that updates each time a compilation is done, you can use VersionUpdaterfrom 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).
如果您想要每次编译完成时都会更新的自动递增数字,您可以使用预构建事件中的VersionUpdater。如果您愿意,您的预构建事件可以检查构建配置,以便版本号只会为发布构建(例如)增加。
回答by user8128167
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;
}
回答by Nacho Coll
[Visual Studio 2017, .csprojproperties]
[Visual Studio 2017,.csproj属性]
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).
要自动更新您的 PackageVersion/Version/AssemblyVersion 属性(或任何其他属性),首先,创建一个新Microsoft.Build.Utilities.Task
类,该类将获取您当前的内部版本号并将更新后的编号发回(我建议为该类创建一个单独的项目)。
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. :)
我手动更新了 major.minor 编号,但让 MSBuild 自动更新内部版本号(1.1. 1、 1.1. 2、 1.1. 3等:)
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:
然后在 MSBuild 进程上调用您最近创建的 Task,在您的 .csproj 文件中添加下一个代码:
<Project Sdk="Microsoft.NET.Sdk">
...
<UsingTask TaskName="RefreshVersion" AssemblyFile="$(MSBuildThisFileFullPath)\..\..\<dll path>\BuildTasks.dll" />
<Target Name="RefreshVersionBuildTask" BeforeTargets="Pack" Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<RefreshVersion CurrentVersionString="$(PackageVersion)">
<Output TaskParameter="NewVersionString" PropertyName="NewVersionString" />
</RefreshVersion>
<Message Text="Updating package version number to $(NewVersionString)..." Importance="high" />
<XmlPoke XmlInputPath="$(MSBuildProjectDirectory)\mustache.website.sdk.dotNET.csproj" Query="/Project/PropertyGroup/PackageVersion" Value="$(NewVersionString)" />
</Target>
...
<PropertyGroup>
..
<PackageVersion>1.1.4</PackageVersion>
..
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).
挑选Visual Studio Pack项目选项时(仅BeforeTargets="Build"
在构建之前更改为执行任务),将触发刷新代码以计算新版本号,并且XmlPoke
任务将相应地更新您的.csproj属性(是的,它将修改文件)。
When working with NuGet libraries, I also send the package to NuGet repository by just adding the next build task to the previous example.
使用 NuGet 库时,我还只需将下一个构建任务添加到上一个示例中,即可将包发送到 NuGet 存储库。
<Message Text="Uploading package to NuGet..." Importance="high" />
<Exec WorkingDirectory="$(MSBuildProjectDirectory)\bin\release" Command="c:\nuget\nuget push *.nupkg -Source https://www.nuget.org/api/v2/package" IgnoreExitCode="true" />
c:\nuget\nuget
is where I have the NuGet client (remember to save your NuGet API key by calling nuget SetApiKey <my-api-key>
or to include the key on the NuGet push call).
c:\nuget\nuget
是我拥有 NuGet 客户端的地方(请记住通过调用nuget SetApiKey <my-api-key>
或在 NuGet 推送调用中包含密钥来保存您的 NuGet API 密钥)。
Just in case it helps someone ^_^.
以防万一它对某人有帮助^_^。