9

I need to read AssemblyFileVersion of dll instead of just Version. I tried:

<Target Name="RetrieveIdentities">
    <GetAssemblyIdentity AssemblyFiles="some.dll">
        <Output
            TaskParameter="Assemblies"
            ItemName="MyAssemblyIdentities"/>
    </GetAssemblyIdentity>
    <Message Text="%(MyAssemblyIdentities.FileVersion)" />
</Target>

The script runs but doesn't output anything. If I change FileVersion to Version it correctly outputs the AssemblyVersion. How do I get AssemblyFileVersion with my script?

O.O
  • 11,077
  • 18
  • 94
  • 182

2 Answers2

5

Borrowing from this answer, I was able to create a custom MSBuild task:

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

And then consume it from within a target:

<GetFileVersion AssemblyPath="some.dll">
  <Output TaskParameter="Version" PropertyName="MyAssemblyFileVersion" />
</GetFileVersion>
<Message Text="File version is $(MyAssemblyFileVersion)" />
Community
  • 1
  • 1
lc.
  • 113,939
  • 20
  • 158
  • 187
4

The MSBuild Extension Pack has a MaxAssemblyFileVersion property that may be useful.

UPDATE:

From the documentation it doesn't look like the GetAssemblyIdentity task returns the FileVersion.

The items output by the Assemblies parameter contain item metadata entries named Version, PublicKeyToken, and Culture.

Also, see the following StackOverflow post.

Read AssemblyFileVersion from AssemblyInfo post-compile

Community
  • 1
  • 1
Garett
  • 16,632
  • 5
  • 55
  • 63
  • This is an old question but I have the same need and wonder if this has been solved reading the DLL not the assemblyinfo.cs file ? –  Aug 25 '15 at 15:43