Get list of projects from Visual Studio .sln file using Microsoft.Build namespace

3k views Asked by At

I figured out how to load a .csproj file using the Microsoft.Build namespace and extract a couple of properties:

var projectCollection = new ProjectCollection();
projectCollection.LoadProject(@"C:\path\to\my.csproj");
string assemblyName = projectCollection.LoadedProjects.First().GetPropertyValue("AssemblyName");
string outputPath = projectCollection.LoadedProjects.First().GetPropertyValue("OutputPath");

Now how can I use Microsoft.Build to load a .sln file and get the list of .csproj files?

1

There are 1 answers

0
Aleksey L. On BEST ANSWER

For parsing solution file you can use SolutionFile class:

var solutionFile = SolutionFile.Parse(@"SOLUTION_PATH.sln");
var projectNames = solutionFile.ProjectsInOrder.Select(p => p.ProjectName).ToList();