如何有一个自动递增版本号(Visual Studio中)? [英] How to have an auto incrementing version number (Visual Studio)?

查看:639
本文介绍了如何有一个自动递增版本号(Visual Studio中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要存储一个整数集是那些获得自动递增的编译时间:

I want to store a set of integers that get auto incremented at build time:

int MajorVersion = 0;
int MinorVersion = 1;
int Revision = 92;

当我编译,它会自动递增修改。当我建立的安装项目,这将增加 MinorVersion (我用手工做这个OK)。 MajorVersion 只会增加手动

When I compile, it would auto-increment Revision. When I build the setup project, it would increment MinorVersion (I'm OK with doing this manually). MajorVersion would only be incremented manually.

然后我可以显示在菜单帮助一个版本号/关于以用户为:

Then I could display a version number in menu Help/About to the user as:


  Version: 0.1.92

如何才能实现这一目标?

How can this be achieved?

这问题问不仅是如何有一个自动递增的版本号,又是如何使用,在code比别人更完整的答案。

This question asks not only how to have an auto-incrementing version number, but also how to use that in code which is a more complete answer than others.

推荐答案

如果您添加一个<一个href=\"http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.assemblyversionattribute.aspx\">AssemblyInfo类项目和修改的AssemblyVersion属性结束与一个星号,例如:

If you add an AssemblyInfo class to your project and amend the AssemblyVersion attribute to end with an asterisk, for example:

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

Visual Studio将增加最终数目为你按照这些规则 (感谢加莱,我有完全错误的!)

Visual studio will increment the final number for you according to these rules (thanks galets, I had that completely wrong!)

要引用这个版本在code,这样你就可以把它显示给用户,可以使用反射。例如,

To reference this version in code, so you can display it to the user, you use reflection. For example,

string version = System.Reflection.Assembly.GetExecutingAssembly()
                                           .GetName()
                                           .Version
                                           .ToString();

两个重要的陷阱,你应该知道

从@ ashes999:

另外值得一提的是,如果两个的AssemblyVersion 的AssemblyFileVersion 被指定,你不会看到这个你.exe文件。

Two important gotchas that you should know

From @ashes999:

It's also worth noting that if both AssemblyVersion and AssemblyFileVersion are specified, you won't see this on your .exe.

仅设置4日数为 * 可不好,因为版本不会总是递增。
第3号是2000年以来的天数,和第4号是几秒钟,因为午夜的数量(除以2)IT不是随机的。因此,如果你在一天晚一天构建的溶液中,并在一天的次日月初,以后的版本将具有较早版本号。我建议您始终使用 X.Y。* 而不是 X.Y.Z。* 因为你的版本号将会随时增加这个样子。

Setting only the 4th number to be * can be bad, as the version won't always increment. The 3rd number is the number of days since the year 2000, and the 4th number is the number of seconds since midnight (divided by 2) [IT IS NOT RANDOM]. So if you built the solution late in a day one day, and early in a day the next day, the later build would have an earlier version number. I recommend always using X.Y.* instead of X.Y.Z.* because your version number will ALWAYS increase this way.

这篇关于如何有一个自动递增版本号(Visual Studio中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆