如何擁有自動遞增版本號(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. 希望有所幫助。

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