How do I programmatically list all projects in a solution?

前端 未结 12 1458
感动是毒
感动是毒 2020-11-29 04:06

How do I programmatically list all of the projects in a solution? I\'ll take a script, command-line, or API calls.

12条回答
  •  被撕碎了的回忆
    2020-11-29 04:35

    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}
        }
    }  
    

提交回复
热议问题