9

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

1 Answer 1

14

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:

enter image description here

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried both the GetAssemblyIdentity and the GetFileVersion task provided in this answer and it looks like the Version from GetAssemblyIdentity doesn't give the detailed version I was looking for but the GetFileVersion task does. Thank you for this solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.