Hack the Build: Cracking MSBuild Project Files

Jomo Fisher — Here’s how you can use the MSBuild Object Model to programmatically access the contents of a project file. This sample console application accepts a single parameter–the full path to some MSBuild project file–and displays all of the assemblies that project references:

using System;

using System.IO;

using Microsoft.Build.BuildEngine;


class MSBuildCracker


      const string msbuildBin

            = @”C:\WINDOWS\Microsoft.NET\Framework\v2.0.40607″;


      static void Main(string[] args)


            //Load the project.

            Engine engine = new Engine(msbuildBin);

            Project project = new Project(engine);



            // Get the references and print them.

            ItemGroup references =



            foreach (Item item in references)






Here’s how you would call the tool:

    MSBuildCracker c:\myprojects\project1\project1.csproj

And this is the result:


You can use this technique to access any of the items (like Reference or Compile) or properties (like AssemblyName or Platform).

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

Comments (5)

  1. Rob Mawston says:

    Some interesting stuff in this. Trying some stuff with this to write changes to an existing project file, but the object model seems to be missing OutputItems (or a way of attaching them to TaskElements).

    Is this just a ‘work in progress’ type issue that’ll be addressed in later betas?

    Thanks for these articles by the way.

  2. Jomo Fisher says:

    Rob, I’m pretty sure this won’t be in Beta2. Hopefully, this is something we can get into the RC. The current plan is that these APIs will not cover everything that the MSBuild language can express (as you’ve discovered).

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

  3. Hi is it possible to get from a solution file also what all project it builds and project interdependencies?

  4. Jomo Fisher says:


    Technically, this is possible, but its not terribly easy. Broadly, here are the steps:

    (1) There’s an API in Microsoft.Build.Conversion.dll that will convert a solution to an in-memory MSBuild project–ProjectFileCOnverter.ConvertInMemory.

    (2) Once you have this, I believe you can grab the names of the project files from the corresponding calls to the "MSBuild task".

    (3) Next, you would need to crack each of these project files in turn and look at their <ProjectReference> items to see what they depend on.

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

  5. Nush says:

    I am not able to get the exact Full Path of the referenced assemblies everytime. I tried reading FullPath Metadata of the Item but it seems not to work everytime. What is the appropriate way to get this?

    Thank you,