How do I programmatically list all projects in a solution?

前端 未结 12 1469
感动是毒
感动是毒 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:21

    I know that this is maybe already answered question, but I would like to share my approach of reading sln file. Also during run time I am determining if project is Test project or not

    function ReadSolutionFile($solutionName)
    {
        $startTime = (Get-Date).Millisecond
        Write-Host "---------------Read Start---------------" 
        $solutionProjects = @()
    
        dotnet  sln "$solutionName.sln" list | ForEach-Object{     
            if($_  -Match ".csproj" )
            {
                #$projData = ($projectString -split '\\')
    
                $proj = New-Object PSObject -Property @{
    
                    Project = [string]$_;
                    IsTestProject =   If ([string]$_ -Match "test") {$True} Else {$False}  
                }
    
                $solutionProjects += $proj
    
            }
        }
    
        Write-Host "---------------Read finish---------------" 
        $solutionProjects
    
        $finishTime = (Get-Date).Millisecond
        Write-Host "Script run time: $($finishTime-$startTime) mil" 
    }
    

    Hope this will be helpfull.

提交回复
热议问题