How do I programmatically list all projects in a solution?

34,428

Solution 1

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

Solution 2

    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

Solution 3

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

Solution 4

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

Solution 5

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

Get-Project -All
Share:
34,428
Kiquenet
Author by

Kiquenet

Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackexchange.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts What have you tried? http://meta.stackexchange.com/questions/122986/is-it-ok-to-leave-what-have-you-tried-comments Asking http://stackoverflow.com/help/asking Answer http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers http://www.enriquepradosvaliente.com http://kiquenet.wordpress.com ◣◥◢◤◢◤◣◥◢◤◢◤◣◥◢◤ ◥◢◤◢◤◣◥◢◤◢◤◣◥◢◤◢ .NET developer and fan of continuous self-improvement and good patterns and practices. Stuff I am interested in: .NET technology stack in general, C#, Powershell and Javascript in particular as languages Test driven development, DI, IoC and mocking frameworks Data access with ORMs and SQL ASP.NET javascript, jQuery and related frontend frameworks Open source projects

Updated on August 21, 2020

Comments

  • Kiquenet
    Kiquenet over 3 years

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

  • Kumar Vaibhav
    Kumar Vaibhav almost 11 years
    The above works but there is a little problem. If your projects are named like "AB.CD" - I mean when the '.' is there then the regex would not recognize those. The following little change would make it work - Regex projReg = new Regex( "Project\(\"\\{[\\w-]*\\}\"\) = \"([\\w _]*.*)\", \"(.*\\.(cs|vcx|vb)proj)\"" , RegexOptions.Compiled);
  • default locale
    default locale over 8 years
    @Kiquenet I'm afraid I didn't understand your comment
  • Kiquenet
    Kiquenet over 8 years
    not any Parser C# class for read sln files ? Similar like stackoverflow.com/questions/707107/… stackoverflow.com/questions/1243022/…
  • Kiquenet
    Kiquenet over 8 years
    Which Visual Studio version ?
  • Alex
    Alex almost 8 years
    This can only be used in a VS extension, so it useless for anybody who wants a stand alone tool for reading source projects in a solution.
  • Scott Mackay
    Scott Mackay almost 8 years
    You can get a reference to a running instance of VS or you can access $dte from the package manager console. If you dont have VS running at all then you need an alternative solution. The new MSBuild packages now contain classes for parsing solution and project files but these are still pre-RTM
  • ncooper09
    ncooper09 about 7 years
    I know this is old, but it is beautiful. Saved me a bunch of time.
  • alastairs
    alastairs about 7 years
    If you need to, you can filter out solution folders by adding an additional Select-String between lines 2 and 3 as follows: Select-String "{2150E333-8FDC-42A3-9474-1A3956D46DE8}" -NotMatch |
  • demokritos
    demokritos almost 7 years
    For VS2017, it is under VS2017 "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\amd64\Microsoft.Bu‌​ild.dll" And annoying, but for sqlprojs files, project type is Unknown [SolutionProjectType]::Unknown
  • PatrickR
    PatrickR over 4 years
  • Badhon Jain
    Badhon Jain over 2 years
    Works like a charm!