代码之家  ›  专栏  ›  技术社区  ›  Gregyski

从AssemblyInfo编译后读取AssemblyFileVersion

  •  13
  • Gregyski  · 技术社区  · 16 年前

    , , , 组装文件修订

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

    程序集版本

    <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>
    

    2 回复  |  直到 16 年前
        1
  •  11
  •   Tom Hunter    13 年前

    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>
    

    用于自动版本控制。


    必须进行一项额外的更改,以使 ApplicationVersion InitialTargets="AfterCompile" <Project... solved by Chao Kuo .

        2
  •  1
  •   kzu    15 年前

    如果使任务从AppDomainIsolatedTask继承,则不需要从流加载程序集。您可以直接使用AppDomain。LoadFrom(文件)。

        3
  •  0
  •   Sayed Ibrahim Hashimi    16 年前