Thursday, October 29, 2009

Configure an MSBuild script to read file data

Today I had to configure an MSBuild script to read some data from a file. First I was looking for a custom task to achieve this goal. But I found that this functionality is out-of-the-box available in MSBuild.

You can read from a file into a variable using:

   1:  <ReadLinesFromFile File="c:\ReleaseNumber.txt">
   2:  <Output TaskParameter="Lines"
   3:  ItemName="ReleaseNumber" />
   4:  </ReadLinesFromFile>

This will store the contents of the file "ReleaseNumber.txt" in a  property "ReleaseNumber". You can then use this property in other places inside your build file:

   1:  "@(ReleaseNumber)"


You can also write to files:

   1:  <WriteLinesToFile File="Log.txt" Lines="This is a log value." Overwrite="false" />

No comments: