MSBuild: How to read Assembly Version and FIle Version from AssmeblyInfo.cs?

北城以北 提交于 2020-05-28 07:57:31

问题


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


回答1:


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

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

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>

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>

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

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

Hope this helps.



来源:https://stackoverflow.com/questions/49624054/msbuild-how-to-read-assembly-version-and-file-version-from-assmeblyinfo-cs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!