编译后,如何读取 .csproj 中的 AssemblyFileVersion 或其组件 AssemblyFileMajorVersion、AssemblyFileMinorVersion、AssemblyFileBuildNumber、AssemblyFileRevision?

我尝试了以下从构建的程序集中提取信息的方法:

<Target Name="AfterCompile">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
         <Output
             TaskParameter="Assemblies"
             ItemName="MyAssemblyIdentities"/>
    </GetAssemblyIdentity>
    <Message Text="AssemblyVersion = %(MyAssemblyIdentities.Version)" />
</Target>

但这会检索 AssemblyVersion 而不是 AssemblyFileVersion。后者似乎没有记录在案的元数据条目。我也试过:
<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks" />
<Target Name="AfterCompile">
    <MSBuild.ExtensionPack.Framework.Assembly TaskAction="GetInfo" NetAssembly="$(TargetPath)">
        <Output TaskParameter="OutputItems" ItemName="Info" />
    </MSBuild.ExtensionPack.Framework.Assembly>
    <Message Text="AssemblyFileVersion = %(Info.FileVersion)" />
</Target>

不幸的是,虽然这会检索到正确的值,但它也会文件锁定程序集,直到 VS2008 关闭。

坦率地说,这也不是我想要的,因为我宁愿直接从 AssemblyInfo.cs 中读取信息。但是,我无法弄清楚如何做到这一点。我认为 MSBuild 扩展中的 AssemblyInfo 是一种方式,但它似乎专注于写入 AssemblyInfo 而不是从中检索值。

我怎样才能最好地做到这一点?

最佳答案

我设法使用自定义任务解决了这个问题。类库 DLL 是这样的(为简洁起见,调整/消除了一些代码):

using System;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;

namespace GetAssemblyFileVersion
{
    public class GetAssemblyFileVersion : ITask
    {
        [Required]
        public string strFilePathAssemblyInfo { get; set; }
        [Output]
        public string strAssemblyFileVersion { get; set; }
        public bool Execute()
        {
            StreamReader streamreaderAssemblyInfo = null;
            Match matchVersion;
            Group groupVersion;
            string strLine;
            strAssemblyFileVersion = String.Empty;
            try
            {
                streamreaderAssemblyInfo = new StreamReader(strFilePathAssemblyInfo);
                while ((strLine = streamreaderAssemblyInfo.ReadLine()) != null)
                {
                    matchVersion = Regex.Match(strLine, @"(?:AssemblyFileVersion\("")(?<ver>(\d*)\.(\d*)(\.(\d*)(\.(\d*))?)?)(?:""\))", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
                    if (matchVersion.Success)
                    {
                        groupVersion = matchVersion.Groups["ver"];
                        if ((groupVersion.Success) && (!String.IsNullOrEmpty(groupVersion.Value)))
                        {
                            strAssemblyFileVersion = groupVersion.Value;
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                BuildMessageEventArgs args = new BuildMessageEventArgs(e.Message, string.Empty, "GetAssemblyFileVersion", MessageImportance.High);
                BuildEngine.LogMessageEvent(args);
            }
            finally { if (streamreaderAssemblyInfo != null) streamreaderAssemblyInfo.Close(); }
            return (true);
        }
        public IBuildEngine BuildEngine { get; set; }
        public ITaskHost HostObject { get; set; }
    }
}

在项目文件中:
<UsingTask AssemblyFile="GetAssemblyFileVersion.dll" TaskName="GetAssemblyFileVersion.GetAssemblyFileVersion" />
<Target Name="AfterCompile">
    <GetAssemblyFileVersion strFilePathAssemblyInfo="$(SolutionDir)\AssemblyInfo.cs">
        <Output TaskParameter="strAssemblyFileVersion" PropertyName="strAssemblyFileVersion" />
    </GetAssemblyFileVersion>
    <Message Text="AssemblyFileVersion = $(strAssemblyFileVersion)" />
</Target>

我已经对此进行了测试,如果您使用 MSBuild.ExtensionPack.VersionNumber.targets 进行自动版本控制,它将读取更新的版本。

显然,这可以轻松扩展,以便将正则表达式从项目文件传递到更通用的自定义任务,以便获得任何文件中的任何匹配项。

2009/09/03 更新:

必须进行一项额外更改才能在每次构建时更新 ApplicationVersionInitialTargets="AfterCompile" 必须添加到 <Project... 。这是 solved by Chao Kuo

关于.net - 编译后从 AssemblyInfo 中读取 AssemblyFileVersion,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1252002/

10-15 11:27