Not so long ago I posted about string manipulations in SQL. Seeing as I recently had to use the MSBuild property functions I thought it best to post a fairly advanced example of using the .Net property functions in MSBuild as most of the examples I have found online are useful to no one. The kind of example I am talking about is found on the MSDN page:

[code langauge=“csharp”]

$(ProjectOutputFolder.Substring(0,3))

e>

I had initially hoped to pass through just $(Date:yyyyMMdd)$(Rev:.r) into a Property, however whenever I did that I would get a build error stating that “The expression “Rev:.r” cannot be evaluated.”. Not to be deterred I knew that I could use the .Net property functions that were made available in MSBuild 4 to extract the date and revision number.

The challenge here was that the build definition is different for every build, so I couldn’t assume to start from a set position: I had to find the position.The first thing I did was create a new Target called Version Number. In this I created a property and added the $(BuildNumber) as its value.


<Target Name="VersionNumber">
<PropertyGroup>
<BuildNumberToSubString>$(BuildNumber)</BuildNumberToSubString>
</PropertyGroup>

I then created another property called AppVersionNumber. Here I would use the Property Functions to get the date and revision number.


<PropertyGroup>
<AppVersionNumber>$(BuildNumberToSubString.Substring($(BuildNumberToSubString.IndexOf('2')),$([MSBuild]::Subtract($(BuildNumberToSubString.length), $(BuildNumberToSubString.IndexOf('2'))))))</AppVersionNumber>
</PropertyGroup>

<Message Text="AppVersionNumber: $(AppVersionNumber)"/>
</Target>

Let’s go through that AppVersionNumber value one item at a time:

<AppVersionNumber>$(BuildNumberToSubString.Substring($(BuildNumberToSubString.IndexOf(‘2’)),

$([MSBuild]::Subtract($(BuildNumberToSubString.length), $(BuildNumberToSubString.IndexOf(‘2’))))))</AppVersionNumber>

Here I am referencing the BuildNumberToSubString Property above and that I am taking a substring of that. The first number I have to give is the starting value. As our BuildNumberFormat is $(BuildDefinitionName)_$(Date:yyyyMMdd)$(Rev:.r) I know that the first “2” I come across is going to be the year. Using the IndexOf function I am able to locate the first “2”. So it’s not the 2nd position of the string but rather the index of the first “2” that is going to be the starting point. So this will definitely stop working come the year 3000. I can live with that.

<AppVersionNumber>$(BuildNumberToSubString.Substring($(BuildNumberToSubString.IndexOf(‘2’)),

$([MSBuild]::Subtract($(BuildNumberToSubString.length), $(BuildNumberToSubString.IndexOf(‘2’))))))</AppVersionNumber>

For the second number, my finishing point, what I do here is find the length of my string, and then subtract from that the index of the first “2”, which again will be the year. So at the end of this I will end up with the value of the length of the date and revision. The value of the second number always has to be not the position in the entire string, but the length that you wish the substring to be.

Here is the target in it’s entirety:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;Project xmlns=&quot;http://schemas.microsoft.com/developer/msbuild/2003&quot;
 ToolsVersion=&quot;4.0&quot; DefaultTargets=&quot;VersionNumber&quot;&gt;
 &lt;Target Name=&quot;VersionNumber&quot;&gt;

&lt;PropertyGroup&gt;
 &lt;BuildNumberToSubString&gt;$(BuildNumber)&lt;/BuildNumberToSubString&gt;
 &lt;/PropertyGroup&gt;
 &lt;PropertyGroup&gt;
 &lt;AppVersionNumber&gt;$(BuildNumberToSubString.Substring($(BuildNumberToSubString.IndexOf('2')),$([MSBuild]::Subtract($(BuildNumberToSubString.length), $(BuildNumberToSubString.IndexOf('2'))))))&lt;/AppVersionNumber&gt;
 &lt;/PropertyGroup&gt;

&lt;BuildStep Name=&quot;UpdateAppVersion&quot;
 Message=&quot;AppVersionNumber: $(AppVersionNumber)&quot;
 TeamFoundationServerUrl=&quot;$(TeamFoundationServerUrl)&quot;
 BuildUri=&quot;$(BuildURI)&quot;
 Condition=&quot;'$(IsDesktopBuild)' != 'true'&quot;&gt;
 &lt;Output TaskParameter=&quot;Id&quot;
 PropertyName=&quot;AppVersionNumberId&quot; /&gt;
 &lt;/BuildStep&gt;

&lt;Message Text=&quot;AppVersionNumber: $(AppVersionNumber)&quot;/&gt;

&lt;BuildStep Id=&quot;$(AppVersionNumberId)&quot;
 Name=&quot;UpdateLocalTestSettings&quot;
 TeamFoundationServerUrl=&quot;$(TeamFoundationServerUrl)&quot;
 BuildUri=&quot;$(BuildURI)&quot;
 Condition=&quot;'$(IsDesktopBuild)' != 'true'&quot;
 Status=&quot;Succeeded&quot; /&gt;
 &lt;/Target&gt;

&lt;/Project&gt;