Hack the Build: Cracking MSBuild Project Files

Hack the Build: Cracking MSBuild Project Files

  • Comments 5

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);

            project.LoadFromFile(args[0]);

 

            // Get the references and print them.

            ItemGroup references =

                  project.GetEvaluatedItemsByType("Reference");

 

            foreach (Item item in references)

            {

                  Console.WriteLine(item.Include);

            }

      }

}

Here's how you would call the tool:

    MSBuildCracker c:\myprojects\project1\project1.csproj

And this is the result:

    System
    System.Deployment
    System.Drawing
    System.Windows.Forms

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.

Leave a Comment
  • Please add 1 and 3 and type the answer here:
  • Post
  • 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.
  • 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.
  • Hi is it possible to get from a solution file also what all project it builds and project interdependencies?
  • Sachit,
    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.
  • 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,

    Nush

Page 1 of 1 (5 items)