天天看点

CLR via C# 读书笔记2-5

程序集的版本资源信息

使用 AL.exe 或 CSC.exe 生成的程序集都会包含一个标准 Win32 版本资源,通过查看这个文件的属性可以查看该信息,也可以在程序代码中调用System.Diagnostics.FileVersionInfo 的静态方法 GetVersionInfo 来查看。图 2-4 显示了之前生成的 MultiFileLibrary.dll 属性对话框:

CLR via C# 读书笔记2-5

可以通过设定自定义属性(custom attributes)来设定版本信息,请参见下面的代码片断:

using System.Reflection;
// FileDescription version information:
[assembly: AssemblyTitle("MultiFileLibrary.dll")]
// Comments version information:
[assembly: AssemblyDescription("This assembly contains MultiFileLibrary's types")]
// CompanyName version information:
[assembly: AssemblyCompany("Wintellect")]
// ProductName version information:
[assembly: AssemblyProduct("Wintellect (R) MultiFileLibrary's Type Library")]
// LegalCopyright version information:
[assembly: AssemblyCopyright("Copyright (c) Wintellect 2013")]
// LegalTrademarks version information:
[assembly:AssemblyTrademark("MultiFileLibrary is a registered trademark of Wintellect")]
// AssemblyVersion version information:
[assembly: AssemblyVersion("3.0.0.0")]

// FILEVERSION/FileVersion version information:
[assembly: AssemblyFileVersion("1.0.0.0")]
// PRODUCTVERSION/ProductVersion version information:
[assembly: AssemblyInformationalVersion("2.0.0.0")]
// Set the Language field (discussed later in the "Culture" section)
[assembly:AssemblyCulture("")]
           

可惜的是Windows 的资源管理器中并没有提供查看以上所有设定值的功能,表2-4 列出了各个版本资源字段与自定义属性之间的对应关系。另外如果在 AL.exe 生成程序集时,可以使用命令开关来设置这些信息,表2-4的第二列记录了这些开关。(C# 编译器并没有这些命令开关)

表2-4

版本资源 AL.exe 命令开关 自定义属性/备注
FILEVERSION /fileversion System.Reflection.AssemblyFileVersionAttribute
PRODUCTVERSION /productversion System.Reflection.AssemblyInformationalVersionAttribute
FILEFLAGSMASK (none) 总为 VS_FFI_FILEFLAGSMASK (定义在 WinVer.h 中,值为 0x0000003F)
FILEFLAGS (none) 总为 0
FILEOS (none) 目前总为 VOS__WINDOWS32.
FILETYPE /target

当 /target:exe 或 /target:winexe 时,设为VFT_APP

当 /target:library 时,设为VFT_DLL

FILESUBTYPE (none) 总为 VFT2_UNKNOWN
AssemblyVersion /version System.Reflection.AssemblyVersionAttribute
Comments /description System.Reflection.AssemblyDescriptionAttribute
CompanyName /company System.Reflection.AssemblyCompanyAttribute
FileDescription /title System.Reflection.AssemblyTitleAttribute
FileVersion /version System.Reflection.AssemblyFileVersionAttribute
InternalName /out 设定为出力文件名 (不含扩展名)
LegalCopyright /copyright System.Reflection.AssemblyCopyrightAttribute
LegalTrademarks /trademark System.Reflection.AssemblyTrademarkAttribute
OriginalFilename /out 设定为出力文件名 (不含路径)
PrivateBuild (none) 总为空
ProductName /product System.Reflection.AssemblyProductAttribute
ProductVersion /productversion System.Reflection.AssemblyInformationalVersionAttribute
SpecialBuild (none) 总为空

使用 Visual Studio 的伙计请注意:建立工程时IDE自动生成一个 AssemblyInfo.cs 文件,你可以直接修改该文件中的版本信息,也可以在工程属性页中修改相关设定,这两者是同步的。

版本号具有固定的格式,可划分为4部分,如表2-5

表2-5

Major Number Minor Number Build Number Revision Number
2 5 719 2

示例的版本号为: 2.5.719.2,前两个数字组成了公共认知的版本号(这是个2.5 版的程序集)。第3个数字用于表示编译的批次,如果你进行每日编译,那么应当每天递增这个值。最后的数字表示某次编译的校订版本,当你一日进行多次编译时或者对应突发bug需要再次编译时可以使用。

在程序集中使用版本号时,你会发现有多达3个版本信息可以设定!他们的不同点参见下文:

■ AssemblyFileVersion

该版本号存在 Win32 版本资源中,仅用来表示信息,CLR 不使用该信息。通常你可以设定这个信息来显示给公众(Windows 资源管理器可查看到该信息),与用户交流时通常也使用这个信息。

■ AssemblyInformationalVersion

该版本号也存在 Win32 版本资源中,也仅用来表示信息,CLR 不使用该信息。它用来指示包含该程序集的产品版本。例如: 一个 2.0 版的产品包含了多个程序集,其中一个程序集是新建的(即:1.0 版的产品中没有包含它),此时你可以设定 AssemblyInformationalVersion 的 major 和 minor 为产品的版本号,然后每次打包整个产品时递增 build 和 revision。

■ AssemblyVersion

该版本存在 AssemblyDef manifest 元数据表中 CLR 把它与强命名一起来唯一标示一个程序集。