C# 使用 Visual Studio 时可以自动增加文件构建版本吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/356543/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:21:22  来源:igfitidea点击:

Can I automatically increment the file build version when using Visual Studio?

c#asp.netvisual-studioversion-controlassemblyinfo

提问by inspite

I was just wondering how I could automaticallyincrement the build (and version?) of my files using Visual Studio (2005).

我只是想知道如何使用 Visual Studio (2005)自动增加文件的构建(和版本?)。

If I look up the properties of say C:\Windows\notepad.exe, the Version tab gives "File version: 5.1.2600.2180". I would like to get these cool numbers in the version of my dll's too, not version 1.0.0.0, which let's face it is a bit dull.

如果我查找 say 的属性C:\Windows\notepad.exe,版本选项卡会给出“文件版本:5.1.2600.2180”。我也想在我的 dll 版本中获得这些很酷的数字,而不是 1.0.0.0 版本,让我们面对它有点乏味。

I tried a few things, but it doesn't seem to be out-of-box functionality, or maybe I'm just looking in the wrong place (as usual).

我尝试了一些东西,但它似乎不是开箱即用的功能,或者我可能只是找错了地方(像往常一样)。

I work with mainly web projects....

我主要从事网络项目....

I looked at both:

我看了两个:

  1. http://www.codeproject.com/KB/dotnet/Auto_Increment_Version.aspx
  2. http://www.codeproject.com/KB/dotnet/build_versioning.aspx
  1. http://www.codeproject.com/KB/dotnet/Auto_Increment_Version.aspx
  2. http://www.codeproject.com/KB/dotnet/build_versioning.aspx

and I couldn't believe it so much effort to do something is standard practice.

我简直不敢相信做某事这么努力是标准做法。

EDIT: It does not work in VS2005 as far I can tell (http://www.codeproject.com/KB/dotnet/AutoIncrementVersion.aspx)

编辑: 据我所知,它在 VS2005 中不起作用(http://www.codeproject.com/KB/dotnet/AutoIncrementVersion.aspx

采纳答案by Sam Meldrum

In visual Studio 2008, the following works.

在 Visual Studio 2008 中,以下工作。

Find the AssemblyInfo.cs file and find these 2 lines:

找到 AssemblyInfo.cs 文件并找到以下两行:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

You could try changing this to:

您可以尝试将其更改为:

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.*")]

But this won't give you the desired result, you will end up with a Product Version of 1.0.*and a File Version of 1.0.0.0. Not what you want!

但这不会给你想要的结果,你最终会得到1.0.*的产品版本和1.0.0.0的文件版本。不是你想要的!

However, if you remove the second of these lines and just have:

但是,如果您删除这些行中的第二行并且只有:

[assembly: AssemblyVersion("1.0.*")]

Then the compiler will set the File Version to be equal to the Product Version and you will get your desired result of an automatically increment product and file version which are in sync. E.g. 1.0.3266.92689

然后编译器会将文件版本设置为等于产品版本,您将获得所需的结果,即自动递增同步的产品和文件版本。例如1.0.3266.92689

回答by Brian Knoblauch

Each time I do a build it auto-increments the least-significant digit.

每次我进行构建时,它都会自动增加最低有效数字。

I don't have any idea how to update the others, but you should at least be seeing that already...

我不知道如何更新其他人,但你至少应该已经看到了......

回答by MusiGenesis

Go to Project | Properties and then Assembly Information and then Assembly Version and put an * in the last or the second-to-last box (you can't auto-increment the Major or Minor components).

转到项目 | 属性,然后是装配信息,然后是装配版本,并在最后一个或倒数第二个框中放置一个 *(您不能自动增加主要或次要组件)。

回答by Bob

To get the version numbers try

要获取版本号,请尝试

 System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
 System.Reflection.AssemblyName assemblyName = assembly.GetName();
 Version version = assemblyName.Version;

To set the version number, create/edit AssemblyInfo.cs

要设置版本号,请创建/编辑 AssemblyInfo.cs

 [assembly: AssemblyVersion("1.0.*")]
 [assembly: AssemblyFileVersion("1.0.*")]

Also as a side note, the third number is the number of days since 2/1/2000 and the fourth number is half of the amount of total seconds in the day. So if you compile at midnight it should be zero.

另外作为旁注,第三个数字是自 2/1/2000 以来的天数,第四个数字是一天中总秒数的一半。所以如果你在午夜编译它应该为零。

回答by Hath

open up the AssemblyInfo.cs file and change

打开 AssemblyInfo.cs 文件并更改

// 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")]

to

[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.0.0")]

you can do this in IDE by going to project -> properties -> assembly information

您可以通过转到项目 -> 属性 -> 程序集信息在 IDE 中执行此操作

This however will only allow you to auto increment the Assembly version and will give you the

然而,这只会允许您自动增加程序集版本,并将为您提供

Assembly File Version: A wildcard ("*") is not allowed in this field

程序集文件版本:此字段中不允许使用通配符(“*”)

message box if you try place a * in the file version field.

如果您尝试在文件版本字段中放置 * 消息框。

So just open up the assemblyinfo.cs and do it manually.

所以只需打开 assemblyinfo.cs 并手动完成。

回答by James Curran

Set the version number to "1.0.*" and it will automatically fill in the last two number with the date (in days from some point) and the time (half the seconds from midnight)

将版本号设置为“1.0.*”,它会自动用日期(从某个点开始的天数)和时间(从午夜开始的半秒)填充最后两个数字

回答by Alex

It is in your project properties under Publish

它位于“发布”下的项目属性中

http://screencast.com/t/Vj7rhqJO
(~ http://screencast.com/t/Vj7rhqJO)

http://screencast.com/t/Vj7rhqJO
(~ http://screencast.com/t/Vj7rhqJO

回答by Dirk Vollmar

Setting a * in the version number in AssemblyInfo or under project properties as described in the other posts does not work with all versions of Visual Studio / .NET.

如其他帖子所述,在 AssemblyInfo 的版本号或项目属性下设置 * 不适用于所有版本的 Visual Studio/.NET。

Afaik it did not work in VS 2005 (but in VS 2003 and VS 2008). For VS 2005 you could use the following: Auto Increment Visual Studio 2005 version build and revision number on compile time.

Afaik 它在 VS 2005 中不起作用(但在 VS 2003 和 VS 2008 中)。对于 VS 2005,您可以使用以下内容:Auto Increment Visual Studio 2005 version build and revision number on compile time

But be aware that changing the version number automatically is not recommended for strong-named assemblies. The reason is that all references to such an assembly must be updated each time the referenced assembly is rebuilt due to the fact that strong-named assembly references are always a reference to a specific assembly version. Microsoft themselves change the version number of the .NET Framework assemblies only if there are changes in interfaces. (NB: I'm still searching for the link in MSDN where I read that.)

但请注意,不建议为强名称程序集自动更改版本号。原因是每次重建引用的程序集时都必须更新对此类程序集的所有引用,因为强命名程序集引用始终是对特定程序集版本的引用。仅当接口发生更改时,Microsoft 自己才会更改 .NET Framework 程序集的版本号。(注意:我仍在 MSDN 中寻找我阅读的链接。)

回答by SeaDrive

As of right now, for my application,

截至目前,对于我的申请,

string ver = Application.ProductVersion;

returns ver = 1.0.3251.27860

返回 ver = 1.0.3251.27860

The value 3251 is the number of days since 1/1/2000. I use it to put a version creation date on the splash screen of my application. When dealing with a user, I can ask the creation date which is easier to communicate than some long number.

值 3251 是自 2000 年 1 月 1 日以来的天数。我用它在我的应用程序的初始屏幕上放置版本创建日期。在与用户打交道时,我可以询问创建日期,这比一些长数字更容易沟通。

(I'm a one-man dept supporting a small company. This approach may not work for you.)

(我是一个支持一家小公司的单人部门。这种方法可能不适合你。)

回答by devstuff

Use the AssemblyInfo task from the MSBuild Community Tasks (http://msbuildtasks.tigris.org/) project, and integrate it into your .csproj/.vbproj file.

使用 MSBuild 社区任务 ( http://msbuildtasks.tigris.org/) 项目中的 AssemblyInfo 任务,并将其集成到您的 .csproj/.vbproj 文件中。

It has a number of options, including one to tie the version number to the date and time of day.

它有许多选项,包括一个将版本号与日期和时间联系起来的选项。

Recommended.

受到推崇的。