Search code examples
visual-studioprojects-and-solutionscsproj

Easy way to add multiple existing .csproj to a Visual Studio Solution?


I've checked out a branch of C# code from source control. It contains maybe 50 projects in various folders. There's no existing .sln file to be found.

I intended to create a blank solution to add existing solutions. The UI only lets me do this one project at a time.

Is there something I'm missing? I'd like to specify a list of *.csproj files and somehow come up with a .sln file that contains all the projects.


Solution

  • A PowerShell implementation that recursively scans the script directory for .csproj files and adds them to a (generated) All.sln:

    $scriptDirectory = (Get-Item $MyInvocation.MyCommand.Path).Directory.FullName
    $dteObj = [System.Activator]::CreateInstance([System.Type]::GetTypeFromProgId("VisualStudio.DTE.12.0"))
    
    $slnDir = ".\"
    $slnName = "All"
    
    $dteObj.Solution.Create($scriptDirectory, $slnName)
    (ls . -Recurse *.csproj) | % { $dteObj.Solution.AddFromFile($_.FullName, $false) }
    
    $dteObj.Solution.SaveAs( (Join-Path $scriptDirectory 'All.sln') ) 
    
    $dteObj.Quit()