Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

msbuild task to read AssemblyFileVersion of dll

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?

like image 535
O.O Avatar asked Sep 01 '11 02:09

O.O


People also ask

What is AssemblyFileVersion?

It's the version number given to file as in file system. It's displayed by Windows Explorer, and never used by . NET framework or runtime for referencing.

How do I find the assembly version of a DLL?

If you reference the dll in Visual Studio right click it (in ProjectName/References folder) and select "Properties" you have "Version" and "Runtime Version" there. In File Explorer when you right click the dll file and select properties there is a "File Version" and "Product Version" there.

What is the difference between assembly version and file version?

AssemblyVersion: Specifies the version of the assembly being attributed. AssemblyFileVersion: Instructs a compiler to use a specific version number for the Win32 file version resource.

Where do I put assembly version?

You can set the assembly version using the AssemblyVersionAttribute. Assembly attributes are usually applied in the AssemblyInfo.


2 Answers

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)" />
like image 154
lc. Avatar answered Oct 16 '22 13:10

lc.


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

like image 34
Garett Avatar answered Oct 16 '22 14:10

Garett