且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何拥有自动递增版本号(Visual Studio)?

更新时间:2023-01-15 10:08:14

如果添加 AssemblyInfo 类添加到您的项目中,并将 AssemblyVersion 属性修改为以星号结尾,例如:

[程序集:AssemblyVersion("2.10.*")]

Visual Studio 将根据 这些为您增加最终数字规则(感谢galets,我完全错了!)

要在代码中引用此版本,以便将其显示给用户,请使用 reflection.例如,

Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;日期时间构建日期 = 新日期时间(2000, 1, 1).AddDays(version.Build).AddSeconds(version.Revision * 2);string displayableVersion = $"{version} ({buildDate})";

你应该知道的三个重要问题

来自@ashes999:

还值得注意的是,如果同时指定了 AssemblyVersionAssemblyFileVersion,您将不会在 .exe 上看到这一点.

来自@BrainSlugs83:

仅将第 4 个数字设置为 * 可能很糟糕,因为版本不会总是递增.第三个数字是自 2000 年以来的天数第四个数字是自午夜以来的秒数(除以 2)[IT IS NOT RANDOM].因此,如果您在一天的晚些时候构建解决方案,并且在第二天的早些时候构建解决方案,则较晚的构建将具有较早的版本号.我建议总是使用 X.Y.* 而不是 X.Y.Z.* 因为你的版本号总是会以这种方式增加.

较新版本的 Visual Studio 出现此错误:

(该主题始于 2009 年)

指定的版本字符串包含通配符,与确定性不兼容.从版本字符串中删除通配符,或禁用此编译的确定性.

请参阅此 SO 答案,其中解释了如何消除确定性(https://***.com/a/58101474/1555612)

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

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

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?

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.

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 will increment the final number for you according to these rules (thanks galets, I had that completely wrong!)

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

Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
DateTime buildDate = new DateTime(2000, 1, 1)
                        .AddDays(version.Build).AddSeconds(version.Revision * 2);
string displayableVersion = $"{version} ({buildDate})";

Three 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.

From @BrainSlugs83:

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.

Newer versions of Visual Studio give this error:

(this thread begun in 2009)

The specified version string contains wildcards, which are not compatible with determinism. Either remove wildcards from the version string, or disable determinism for this compilation.

See this SO answer which explains how to remove determinism (https://***.com/a/58101474/1555612)