2

I have setup.exe. This is a Basic MSI Installation package. I use InstallShield to create it. How can I get its FileVersion in MSBuild script? I cannot use GetAssemblyIdentity task

    <GetAssemblyIdentity AssemblyFiles="setup.exe">
        <Output
            TaskParameter="Assemblies"
            ItemName="MyAssemblyIdentities"/>
    </GetAssemblyIdentity>

because my setup.exe is not an assembly and doesn't contain an assembly manifest, so an error appears if invoke this task:

Could not load file or assembly 'setup.exe' or one of its dependencies. The module was expected to contain an assembly manifest.

AndreyS
  • 365
  • 7
  • 18

1 Answers1

3

The FileVersionInfo class provides this functionality, and inline code allows using it from MSBuild directly:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >

  <UsingTask TaskName="GetVersion" TaskFactory="CodeTaskFactory"
             AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
    <ParameterGroup>
      <TheFile Required="true" ParameterType="System.String"/>
      <TheVersion ParameterType="System.String" Output="true" />
    </ParameterGroup>
    <Task>
      <Code Type="Fragment" Language="cs">
        <![CDATA[
          TheVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo( TheFile ).FileVersion;
         ]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="DoGetVersion">
    <GetVersion TheFile="$(MyAssemblyIdentities)">
      <Output PropertyName="FetchedVersion" TaskParameter="TheVersion" />  
    </GetVersion>
    <Message Text="Version = $(FetchedVersion)" />
  </Target>

</Project>
stijn
  • 34,664
  • 13
  • 111
  • 163
  • Can you explain this a bit more? – Stefan Vasiljevic Apr 02 '15 at 17:15
  • @StefanVasiljevic what exactly do you want to know? The GetVersion/CodeTaskFactory part is a way to make msbuild run C# code, calling GetVersionInfo in this case. DoGetVersion just calls GetVersion and siaplys the result. – stijn Apr 03 '15 at 07:10
  • 1
    I figured it out. The problem was in the TheFile part. I actually forgot to replace that with the actual path to the file I want to get the version of. Thx :) – Stefan Vasiljevic Apr 04 '15 at 13:15