10

I'm using Msbuild to compile and generate .zip files and installers and I need the version number of my assembyInfo.

I'm using this code.

<Target Name="getversion">
    <GetAssemblyIdentity AssemblyFiles="$(BuildDir)\myprogram.exe">
        <Output TaskParameter="Assemblies" ItemName="fooAssemblyInfo"/>
    </GetAssemblyIdentity>
    <Message Text="Version = %(fooAssemblyInfo.Version)"/>
</Target>

But this returns Version = 2.0.0.29110, I need just the minor and major version.

Is there any way to read the assembyInfo.cs information without a custom task?

stijn
  • 34,664
  • 13
  • 111
  • 163
Carlos Garces
  • 829
  • 1
  • 11
  • 24

3 Answers3

18

Finally I have used this code that not require additional task libraries

<Target Name="getversion">
    <GetAssemblyIdentity AssemblyFiles="$(BuildDir)\myfile.exe">
        <Output TaskParameter="Assemblies" ItemName="myAssemblyInfo"/>
    </GetAssemblyIdentity>
    <PropertyGroup>
        <Pattern>(\d+)\.(\d+)</Pattern>
        <In>%(myAssemblyInfo.Version)</In>
        <OutVersion>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</OutVersion>
    </PropertyGroup>
</Target>
Carlos Garces
  • 829
  • 1
  • 11
  • 24
  • For anyone having troubles like I did, I had to include the references Microsoft.Build and Microsoft.Build.Framework for this to work, otherwise, it was giving me an empty string. – matth Jun 23 '15 at 19:14
  • How do you reference Microsoft.Build in the wixproject? – rollsch Sep 18 '17 at 01:02
  • Doesn't work for me. Just returns the empty string. Cannot add Microsoft.Build because my project is for .NET Framework 2.0 – Doctor Coder May 18 '18 at 11:36
13

It can be done using MSBuild Property Functions described here: https://msdn.microsoft.com/en-us/library/dd633440%28v=vs.120%29.aspx

<Target Name="getversion">
    <GetAssemblyIdentity AssemblyFiles="$(BuildDir)\myprogram.exe">
        <Output TaskParameter="Assemblies" ItemName="fooAssemblyInfo"/>
    </GetAssemblyIdentity>

    <Message Text="Version = $([System.Version]::Parse(%(fooAssemblyInfo.Version)).ToString(2))" Importance="high" />
</Target>

Output:

Done executing task "GetAssemblyIdentity".
Task "Message"
    Task Parameter:Text=Version = 12.0
    Task Parameter:Importance=high
    Version = 12.0
Done executing task "Message".
Sergei Zinovyev
  • 1,238
  • 14
  • 14
10

If you're using MsBuild 4.0 you can write your own inline custom Task to get this done.

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

  <ParameterGroup>
    <AssemblyPath ParameterType="System.String" Required="true" />
    <MajorVersion ParameterType="System.Int32" Output="true" />
    <MinorVersion ParameterType="System.Int32" Output="true" />
    <BuildVersion ParameterType="System.Int32" 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);

        Version v = Version.Parse(FileVersionInfo.GetVersionInfo(this.AssemblyPath).FileVersion);

        this.MajorVersion = v.Major;
        this.MinorVersion = v.Minor;
        this.BuildVersion = v.Build;    
      ]]>
    </Code>
  </Task>
</UsingTask>

and then reference the inline task elsewhere in your build script...

<GetVersionParts AssemblyPath="$(OutputDirAbsolute)/MyAssembly.dll">
  <Output TaskParameter="MajorVersion" PropertyName="MajorVersionNumber" />
  <Output TaskParameter="MinorVersion" PropertyName="MinorVersionNumber" />
  <Output TaskParameter="BuildVersion" PropertyName="BuildVersionNumber" />
</GetVersionParts>

Using the inline Task, you have can have easy access to each part of the assembly's build number. e.g $(MajorVersionNumber)

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Dav Evans
  • 4,031
  • 7
  • 41
  • 60