我想存储一组在构建时自动递增的整数:
int MajorVersion = 0;
int MinorVersion = 1;
int Revision = 92;
当我编译时,它会自动增加Revision。当我构建安装项目时,它会增加MinorVersion(我可以手动这样做)。MajorVersion只能手动递增。
然后我可以在菜单Help/About中显示一个版本号给用户:
Version: 0.1.92
如何实现这一目标?
这个问题不仅问如何有一个自动递增的版本号,还问如何在代码中使用它,这是一个比其他问题更完整的答案。
以下是来自MSDN的AssemblyInfo.cs上的引用:
You can specify all the values or you
can accept the default build number,
revision number, or both by using an
asterisk (). For example,
[assembly:AssemblyVersion("2.3.25.1")]
indicates 2 as the major version, 3 as
the minor version, 25 as the build
number, and 1 as the revision number.
A version number such as
[assembly:AssemblyVersion("1.2.")]
specifies 1 as the major version, 2 as
the minor version, and accepts the
default build and revision numbers. A
version number such as
[assembly:AssemblyVersion("1.2.15.*")]
specifies 1 as the major version, 2 as
the minor version, 15 as the build
number, and accepts the default
revision number. The default build
number increments daily. The default
revision number is random
这就是说,如果你放一个1.1。*进入组装信息,只有构建号会自动增加,它不会发生在每个构建后,而是每天。修订号会在每次构建时改变,但是是随机的,而不是以递增的方式。
这对于大多数用例来说可能已经足够了。如果这不是你想要的,你不得不写一个脚本,它将自动增加版本#在预构建步骤
以下是来自MSDN的AssemblyInfo.cs上的引用:
You can specify all the values or you
can accept the default build number,
revision number, or both by using an
asterisk (). For example,
[assembly:AssemblyVersion("2.3.25.1")]
indicates 2 as the major version, 3 as
the minor version, 25 as the build
number, and 1 as the revision number.
A version number such as
[assembly:AssemblyVersion("1.2.")]
specifies 1 as the major version, 2 as
the minor version, and accepts the
default build and revision numbers. A
version number such as
[assembly:AssemblyVersion("1.2.15.*")]
specifies 1 as the major version, 2 as
the minor version, 15 as the build
number, and accepts the default
revision number. The default build
number increments daily. The default
revision number is random
这就是说,如果你放一个1.1。*进入组装信息,只有构建号会自动增加,它不会发生在每个构建后,而是每天。修订号会在每次构建时改变,但是是随机的,而不是以递增的方式。
这对于大多数用例来说可能已经足够了。如果这不是你想要的,你不得不写一个脚本,它将自动增加版本#在预构建步骤
这是我对T4建议的实现…这将在每次构建项目时增加构建号,而不考虑所选的配置(即调试|发布),并且在每次进行发布构建时增加修订号。您可以通过应用程序➤组装信息继续更新主版本号和次版本号…
为了更详细地解释,这将读取现有的AssemblyInfo.cs文件,并使用regex来查找AssemblyVersion信息,然后根据来自TextTransform.exe的输入增加修订和构建号。
Delete your existing AssemblyInfo.cs file.
Create a AssemblyInfo.tt file in its place. Visual Studio should create AssemblyInfo.cs and group it with the T4 file after you save the T4 file.
<#@ 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;
#>
Add this to your pre-build event:
"%CommonProgramFiles(x86)%\microsoft shared\TextTemplating\$(VisualStudioVersion)\TextTransform.exe" -a !!BuildConfiguration!$(Configuration) "$(ProjectDir)Properties\AssemblyInfo.tt"
使用AssemblyInfo.cs
在App_Code中创建文件,并填写以下内容或使用谷歌获取其他属性/属性。
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是您真正需要的部分。
然后,如果你在一个网站上工作,在任何aspx页面或控件中,你可以在< page >标签中添加以下内容:
CompilerOptions="<folderpath>\App_Code\AssemblyInfo.cs"
(替换文件夹路径与适当的变量当然)。
我认为你不需要以任何方式为其他类添加编译器选项;App_Code中的所有文件在编译时都应该收到版本信息。
希望这能有所帮助。