How do I programmatically list all of the projects in a solution? I\'ll take a script, command-line, or API calls.
The trick is to choose the right MsBuild.dll. Under VS2017 it is indeed "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\amd64\Microsoft.Build.dll" (Dont use the standard Msbuild ddl in references. Browse to this path)
c#:
var solutionFile =
SolutionFile.Parse(@"c:\NuGetApp1\NuGetApp1.sln");//your solution full path name
var projectsInSolution = solutionFile.ProjectsInOrder;
foreach(var project in projectsInSolution)
{
switch (project.ProjectType)
{
case SolutionProjectType.KnownToBeMSBuildFormat:
{
break;
}
case SolutionProjectType.SolutionFolder:
{
break;
}
}
}
powershell:
Add-Type -Path (${env:ProgramFiles(x86)} + '\Microsoft Visual
Studio\2017\Professional\MSBuild\15.0\Bin\amd64\Microsoft.Build.dll')
$slnPath = 'c:\NuGetApp1\NuGetApp1.sln'
$slnFile = [Microsoft.Build.Construction.SolutionFile]::Parse($slnPath)
$pjcts = $slnFile.ProjectsInOrder
foreach ($item in $pjcts)
{
switch($item.ProjectType)
{
'KnownToBeMSBuildFormat'{Write-Host Project : $item.ProjectName}
'SolutionFolder'{Write-Host Solution Folder : $item.ProjectName}
}
}