Hack the Build: Programmatically Converting Older VS .NET Projects to MSBuild

Jomo Fisher--There are plenty of reasons you might want to programmatically convert projects from VS .NET 2003 format to MSBuild format. Maybe you have a bunch of old .csproj files and you'd like to convert them in batch. Maybe you'd like to keep your projects in VS7 format and convert them to MSBuild format on the fly. These are definitely Hack-the-Build-worthy pursuits, so let's dig in.

MSBuild ships with an assembly called Microsoft.Build.Conversion.dll which contains the code that Visual Studio .NET 2005 uses to convert older C# and VB projects into MSBuild format (unfortunately C++ is not supported).

The code for calling this API is so simple, I can paste it right here:

/*

Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm

Written by Jomo Fisher

*/

using System;

using System.Text;

using Microsoft.Build.Conversion;

class ConvertProject

{

     static void Main(string[] args)

     {

          ProjectFileConverter c =

           new ProjectFileConverter();

          c.OldProjectFile = args[0];

          c.NewProjectFile = args[1];

          c.Convert();

     }

}

To invoke, just call the code with source and destination file names:

 

   ConvertProject VS7.csproj VS8.csproj

 

A few things to note:

· You'll need to add a reference to Microsoft.Build.Conversion in your project.

· You'll need to manually copy the DLL Microsoft.Build.Conversion.dll into the same directory as the application built from the code above. Chances are that we'll have this DLL in the GAC for the next Beta, but for now you'll just need to manually copy it.

· This API is undocumented and not supported for calling directly. Caveat emptor, etc.

If you subscribe to other Microsoft Visual Studio related blogs you may have noticed a lull in the postings. This is because we're working like caffeinated woodpeckers getting the next Beta of VS ready. Thankfully, the tryptophan has cut through my (legal) stimulant haze long enough this morning to allow me to post a hack for you—I hope you find it worthy.

One thing worth mentioning as we head down the homestretch: We've used Test Driven Development with NUnit throughout most of the MSBuild development cycle. We're really seeing a big pay-off in the end-game--we're able to make difficult changes in complex code with confidence that we're not breaking functionality. In other words, I've tasted the Kool-Aid and can only say “Oh, Yeaahh!”.

This posting is provided "AS IS" with no warranties, and confers no rights.