I recently had a need to “capture the output” of a command line tool, but within a MSBuild Custom Task (context).

While I know there are “msbuild’ish” ways to accomplish the below example (getting a list of directories from a “dir” command), the below is an ~~example~~ of how to capture the output of a command line call.

But what I did do is package this up in a smaller (more digestable?) example as seen below.
And I remember when I first started with writing my own custom MSBuild tasks that what is obvious to me now, was not obvious when I first started.

So, in a nutshell:
“CollectedOutput” will be the property that has all the contents for the ‘dir “c:\”‘.

The CSharp code will need to be put into a “Class Library” DotNet csproj of course.
If you want the example to run “out of the box”, then name the new csproj with the name “GranadaCoder.Framework.CrossDomain.MSBuild” (If you don’t do this, you’ll need to adjust the name of the .dll mentioned in the .msbuild file).
After compiling, place the .dll in the same folder as the .msbuild and .bat file.

//START CSharp Code//

namespace GranadaCoder.Framework.CrossDomain.MSBuild.Tasks.Temp
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Reflection;

using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.Diagnostics;

//Start CommandLineDirectoryTaskTest.bat file
set msBuildDir=%WINDIR%\Microsoft.NET\Framework\v3.5
set msBuildDir=%WINDIR%\Microsoft.NET\Framework\v2.0.50727
set msBuildDir=%WINDIR%\Microsoft.NET\Framework\v3.5