1

I am working on a .net application (VS2010) with x no. of solutions and variable no. of projects in those solutions. I need to check if the project properties (specific to a certain number of projects) are homogenous, and also check/validate the reference paths during the build. Is there an API that does this? If not, how do i go about building one?

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
aromore
  • 977
  • 2
  • 10
  • 25
  • 1
    although the solution files are propriatry (but readable) the project files (*.csproj or *.vbproj) are xml (msbuild files). You could write a tool that does a xpath lookup in those files. – rene Aug 03 '12 at 20:59

1 Answers1

3

You can use MSBuild framework to do the parsing and perform evaluation of the project files. The main classes you need to use are ProjectCollection and Project.

But first you will need to deal with your .sln files. Since API cannot load .sln files directly, you will need first to convert your .sln files to projects files, that API can load. The method is described here. You will get a .sln.metaproj files after conversion, that are equivalent representation of .sln, but have MSBuild format. After that you can parse .sln.metaproj files and referenced projects and evaluate properties you need. This sample prints out the OutputPath property evaluation for Debug|AnyCPU configuration of all projects in the solution:

    Dictionary<string, string> globalProperties = new Dictionary<string, string>();

    globalProperties.Add("Configuraion", "Debug");
    globalProperties.Add("Platform", "AnyCPU");

    ProjectCollection pc = new ProjectCollection(globalProperties);

    Project sln = pc.LoadProject(@"my_directory\My_solution_name.sln.metaproj", "4.0");

    foreach (ProjectItem pi in sln.Items)
    {
        if (pi.ItemType == "ProjectReference")
        {
            Project p = pc.LoadProject(pi.EvaluatedInclude);
            ProjectProperty pp = p.GetProperty("OutputPath");
            if (pp != null)
            {
                Console.WriteLine("Project=" + pi.EvaluatedInclude + " OutputPath=" + pp.EvaluatedValue);
            }
        }
    }
seva titov
  • 11,720
  • 2
  • 35
  • 54