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

本文翻译自:How to have an auto incrementing version number (Visual Studio)? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

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 . 当我编译时,它会自动增加Revision When I build the setup project, it would increment MinorVersion (I'm OK with doing this manually). 当我构建安装项目时,它会增加MinorVersion (我可以手动执行此操作)。 MajorVersion would only be incremented manually. MajorVersion只会手动增加。

Then I could display a version number in menu Help/About to the user as: 然后我可以在菜单Help / About中向用户显示版本号:

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. 这个问题不仅要求如何使用自动递增版本号,还要求如何在代码中使用它,这是一个比其他更完整的答案。


#1楼

参考:https://stackoom.com/question/3T57/如何拥有自动递增版本号-Visual-Studio-重复


#2楼

This is my implementation of the T4 suggestion... This will increment the build number every time you build the project regardless of the selected configuration (ie Debug|Release), and it will increment the revision number every time you do a Release build. 这是我对T4建议的实现...这将在每次构建项目时增加构建号,而不管所选的配置(即Debug | Release),并且每次执行Release构建时它都会增加修订号。 You can continue to update the major and minor version numbers through Application ➤ Assembly Information... 您可以通过应用程序➤装配信息继续更新主要和次要版本号...

To explain in more detail, this will read the existing AssemblyInfo.cs file, and use regex to find the AssemblyVersion information and then increment the revision and build numbers based on input from TextTransform.exe . 要更详细地解释,这将读取现有的AssemblyInfo.cs文件,并使用正则表达式查找AssemblyVersion信息,然后根据TextTransform.exe输入增加修订版本和内部版本号。

  1. Delete your existing AssemblyInfo.cs file. 删除现有的AssemblyInfo.cs文件。
  2. Create a AssemblyInfo.tt file in its place. 在其位置创建AssemblyInfo.tt文件。 Visual Studio should create AssemblyInfo.cs and group it with the T4 file after you save the T4 file. 保存T4文件后,Visual Studio应创建AssemblyInfo.cs并将其与T4文件分组。

     <#@ template debug="true" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Text.RegularExpressions" #> <# string output = File.ReadAllText(this.Host.ResolvePath("AssemblyInfo.cs")); Regex pattern = new Regex("AssemblyVersion\\\\(\\"(?<major>\\\\d+)\\\\.(?<minor>\\\\d+)\\\\.(?<revision>\\\\d+)\\\\.(?<build>\\\\d+)\\"\\\\)"); MatchCollection matches = pattern.Matches(output); if( matches.Count == 1 ) { major = Convert.ToInt32(matches[0].Groups["major"].Value); minor = Convert.ToInt32(matches[0].Groups["minor"].Value); build = Convert.ToInt32(matches[0].Groups["build"].Value) + 1; revision = Convert.ToInt32(matches[0].Groups["revision"].Value); if( this.Host.ResolveParameterValue("-","-","BuildConfiguration") == "Release" ) revision++; } #> using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Resources; // General Information [assembly: AssemblyTitle("Insert title here")] [assembly: AssemblyDescription("Insert description here")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Insert company here")] [assembly: AssemblyProduct("Insert product here")] [assembly: AssemblyCopyright("Insert copyright here")] [assembly: AssemblyTrademark("Insert trademark here")] [assembly: AssemblyCulture("")] // Version informationr( [assembly: AssemblyVersion("<#= this.major #>.<#= this.minor #>.<#= this.revision #>.<#= this.build #>")] [assembly: AssemblyFileVersion("<#= this.major #>.<#= this.minor #>.<#= this.revision #>.<#= this.build #>")] [assembly: NeutralResourcesLanguageAttribute( "en-US" )] <#+ int major = 1; int minor = 0; int revision = 0; int build = 0; #> 
  3. Add this to your pre-build event: 将此添加到您的预构建事件:

     "%CommonProgramFiles(x86)%\\microsoft shared\\TextTemplating\\$(VisualStudioVersion)\\TextTransform.exe" -a !!BuildConfiguration!$(Configuration) "$(ProjectDir)Properties\\AssemblyInfo.tt" 

#3楼

  • Star in version (like "2.10.3.*") - it is simple, but the numbers are too large 版本中的明星(如“2.10.3。*”) - 很简单,但数字太大了

  • AutoBuildVersion - looks great but its dont work on my VS2010. AutoBuildVersion - 看起来很棒,但它不适用于我的VS2010。

  • @DrewChapin's script works, but I can not in my studio set different modes for Debug pre-build event and Release pre-build event. @DrewChapin的脚本有效,但我不能在我的工作室中为Debug预构建事件和Release预构建事件设置不同的模式。

so I changed the script a bit... commamd: 所以我改变了脚本... commamd:

"%CommonProgramFiles(x86)%\microsoft shared\TextTemplating\10.0\TextTransform.exe" -a !!$(ConfigurationName)!1 "$(ProjectDir)Properties\AssemblyInfo.tt"

and script (this works to the "Debug" and "Release" configurations): 和脚本(这适用于“调试”和“发布”配置):

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Windows.Forms" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#
    int incRevision = 1;
    int incBuild = 1;

    try { incRevision = Convert.ToInt32(this.Host.ResolveParameterValue("","","Debug"));} catch( Exception ) { incBuild=0; }
    try { incBuild = Convert.ToInt32(this.Host.ResolveParameterValue("","","Release")); } catch( Exception ) { incRevision=0; }
    try {
        string currentDirectory = Path.GetDirectoryName(Host.TemplateFile);
        string assemblyInfo = File.ReadAllText(Path.Combine(currentDirectory,"AssemblyInfo.cs"));
        Regex pattern = new Regex("AssemblyVersion\\(\"\\d+\\.\\d+\\.(?<revision>\\d+)\\.(?<build>\\d+)\"\\)");
        MatchCollection matches = pattern.Matches(assemblyInfo);
        revision = Convert.ToInt32(matches[0].Groups["revision"].Value) + incRevision;
        build = Convert.ToInt32(matches[0].Groups["build"].Value) + incBuild;
    }
    catch( Exception ) { }
#>
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Game engine. Keys: F2 (Debug trace), F4 (Fullscreen), Shift+Arrows (Move view). ")]
[assembly: AssemblyProduct("Game engine")]
[assembly: AssemblyDescription("My engine for game")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyCopyright("Copyright © Name 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type. Only Windows
// assemblies support COM.
[assembly: ComVisible(false)]

// On Windows, the following GUID is for the ID of the typelib if this
// project is exposed to COM. On other platforms, it unique identifies the
// title storage container when deploying this assembly to the device.
[assembly: Guid("00000000-0000-0000-0000-000000000000")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
[assembly: AssemblyVersion("0.1.<#= this.revision #>.<#= this.build #>")]
[assembly: AssemblyFileVersion("0.1.<#= this.revision #>.<#= this.build #>")]

<#+
    int revision = 0;
    int build = 0;
#>

#4楼

您可以使用构建脚本(如构建版本控制)执行更高级的版本控制


#5楼

If you put an asterisk in for build and revision visual studio uses the number of days since Jan. 1st 2000 as the build number, and the number of seconds since midnight divided by 2 as the revision. 如果您在构建和修订中添加星号,则visual studio将使用自2000年1月1日以来的天数作为内部版本号,并将自午夜以来的秒数除以2作为修订版。

A MUCH better life saver solution is http://autobuildversion.codeplex.com/ 更好的救生解决方案是http://autobuildversion.codeplex.com/

It works like a charm and it's VERY flexible. 它就像一个魅力,它非常灵活。


#6楼

Use AssemblyInfo.cs 使用AssemblyInfo.cs

Create the file in App_Code: and fill out the following or use Google for other attribute/property possibilities. 在App_Code中创建文件:并填写以下内容或使用Google获取其他属性/属性的可能性。

AssemblyInfo.cs AssemblyInfo.cs中

using System.Reflection;

[assembly: AssemblyDescription("Very useful stuff here.")]
[assembly: AssemblyCompany("companyname")]
[assembly: AssemblyCopyright("Copyright © me 2009")]
[assembly: AssemblyProduct("NeatProduct")]
[assembly: AssemblyVersion("1.1.*")]

AssemblyVersion being the part you are really after. AssemblyVersion是你真正追求的部分。

Then if you are working on a website, in any aspx page, or control, you can add in the <Page> tag, the following: 然后,如果您正在使用网站,任何aspx页面或控件,您可以添加<Page>标记,如下所示:

CompilerOptions="<folderpath>\App_Code\AssemblyInfo.cs"

(replacing folderpath with appropriate variable of course). (当然,用适当的变量替换folderpath)。

I don't believe you need to add compiler options in any manner for other classes; 我不认为你需要以任何方式为其他类添加编译器选项; all the ones in the App_Code should receive the version information when they are compiled. App_Code中的所有内容在编译时都应该收到版本信息。

Hope that helps. 希望有所帮助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章