How do I programmatically list all projects in a solution?

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

    Here's a PowerShell script that retrieves project details from a .sln file:

    Get-Content 'Foo.sln' |
      Select-String 'Project\(' |
        ForEach-Object {
          $projectParts = $_ -Split '[,=]' | ForEach-Object { $_.Trim('[ "{}]') };
          New-Object PSObject -Property @{
            Name = $projectParts[1];
            File = $projectParts[2];
            Guid = $projectParts[3]
          }
        }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 04:25

    Currently you can use Package Manager Console in VS to obtain that info. Use powershell Get-Project command

    Get-Project -All
    
    0 讨论(0)
  • 2020-11-29 04:29

    You can use the EnvDTE.Solution.Projects object to programmatically get access to the projects in a solution.

    One gotcha though is that if you have any SolutionFolders in your solution, any projects in these folders are not shown in the above collection.

    I've written an article including a code sample on how to get all projects regardless of any solutionfolders

    0 讨论(0)
  • 2020-11-29 04:30
        var Content = File.ReadAllText(SlnPath);
        Regex projReg = new Regex(
            "Project\\(\"\\{[\\w-]*\\}\"\\) = \"([\\w _]*.*)\", \"(.*\\.(cs|vcx|vb)proj)\""
            , RegexOptions.Compiled);
        var matches = projReg.Matches(Content).Cast<Match>();
        var Projects = matches.Select(x => x.Groups[2].Value).ToList();
        for (int i = 0; i < Projects.Count; ++i)
        {
            if (!Path.IsPathRooted(Projects[i]))
                Projects[i] = Path.Combine(Path.GetDirectoryName(SlnPath),
                    Projects[i]);
            Projects[i] = Path.GetFullPath(Projects[i]);
        }
    

    Edit: Amended the regex to include the ".*" as per the comment by Kumar Vaibhav

    0 讨论(0)
  • 2020-11-29 04:32

    There's a really elegant solution here: Parsing Visual Studio Solution files

    The answer by John Leidegren involves wrapping the internal Microsoft.Build.Construction.SolutionParser class.

    0 讨论(0)
提交回复
热议问题