MSBuild:如何从AssmeblyInfo.cs中读取程序集版本和文件版本? [英] MSBuild: How to read Assembly Version and FIle Version from AssmeblyInfo.cs?

查看:140
本文介绍了MSBuild:如何从AssmeblyInfo.cs中读取程序集版本和文件版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MSBuild脚本如何从AssemblyInfo.cs中获取AssmeblyVersion和FileVersion?

Using MSBuild script how can we get AssmeblyVersion and FileVersion from AssemblyInfo.cs?

推荐答案

使用MSBuild脚本如何从AssemblyInfo.cs中获取AssmeblyVersion和FileVersion?

Using MSBuild script how can we get AssmeblyVersion and FileVersion from AssemblyInfo.cs?

要通过MSBuild获取AssmeblyVersion,可以使用 GetAssemblyIdentity 任务.

To get the AssmeblyVersion via MSBuild, you can use GetAssemblyIdentity Task.

要实现此目的,请卸载您的项目.然后在项目的最后,在结束标签</Project>之前,放置在脚本下面:

To accomplish this, unload your project. Then at the very end of the project, just before the end-tag </Project>, place below scripts:

  <Target Name="GetAssmeblyVersion" AfterTargets="Build">
    <GetAssemblyIdentity
        AssemblyFiles="$(TargetPath)">
      <Output
          TaskParameter="Assemblies"
          ItemName="MyAssemblyIdentities"/>
    </GetAssemblyIdentity>

    <Message Text="Assmebly Version: %(MyAssemblyIdentities.Version)"/>
  </Target>

要获取FileVersion,可以添加自定义 MSBuild内联任务为此,请编辑您的项目文件.csproj,添加以下代码:

To get the FileVersion, you could add a custom MSBuild Inline Tasks to get this, edit your project file .csproj, add following code:

  <UsingTask
TaskName="GetFileVersion"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">

    <ParameterGroup>
      <AssemblyPath ParameterType="System.String" Required="true" />
      <Version ParameterType="System.String" Output="true" />
    </ParameterGroup>
    <Task>
      <Using Namespace="System.Diagnostics" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[
      Log.LogMessage("Getting version details of assembly at: " + this.AssemblyPath, MessageImportance.High);

      this.Version = FileVersionInfo.GetVersionInfo(this.AssemblyPath).FileVersion;  
    ]]>
      </Code>
    </Task>
  </UsingTask>



  <Target Name="GetFileVersion" AfterTargets="Build">

  <GetFileVersion AssemblyPath="$(TargetPath)">
    <Output TaskParameter="Version" PropertyName="MyAssemblyFileVersion" />
  </GetFileVersion>
  <Message Text="File version is $(MyAssemblyFileVersion)" />
  </Target>

有了这两个目标之后,您将从AssemblyInfo.cs获得AssmeblyVersionFileVersion:

After with those two targets, you will get the AssmeblyVersion and FileVersion from AssemblyInfo.cs:

注意:如果要获取特定dll的版本,可以将AssemblyFiles="$(TargetPath)"更改为:

Note:If you want to get the version of a specific dll, you can change the AssemblyFiles="$(TargetPath)" to the:

<PropertyGroup>
    <MyAssemblies>somedll\the.dll</MyAssemblies>
</PropertyGroup>

AssemblyFiles="@(MyAssemblies)"

希望这会有所帮助.

这篇关于MSBuild:如何从AssmeblyInfo.cs中读取程序集版本和文件版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆