Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 952
Michael J Swart Avatar asked Dec 11 '09 21:12

Michael J Swart


People also ask

How do I add multiple existing projects in Visual Studio?

To add existing projects to the solution: Right click the Solution node in Solution Explorer select Add -> Multiple Projects.

How do I add multiple project codes in Visual Studio?

You can use drag and drop to add folders to a workspace. Drag a folder to the File Explorer to add it to the current workspace. You can even select and drag multiple folders. Note: Dropping a single folder into the editor region of VS Code will still open the folder in single folder mode.

How do I add a .csproj file to a solution?

Adding a project to a solution file Once you have a solution file, you can add a project to it using the sln add command, and provide the path to the project's . csproj file. This will add the project to an existing solution file in the current folder.


2 Answers

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() 
like image 71
Mikkel Christensen Avatar answered Sep 19 '22 22:09

Mikkel Christensen


A C# implementation that produces an executable, which creates a solution containing all unique *.csproj files from the directory and subdirectories it is executed in.

class Program {   static void Main(string[] args)   {     using (var writer = new StreamWriter("All.sln", false, Encoding.UTF8))     {       writer.WriteLine("Microsoft Visual Studio Solution File, Format Version 11.00");       writer.WriteLine("# Visual Studio 2010");        var seenElements = new HashSet<string>();       foreach (var file in (new DirectoryInfo(System.IO.Directory.GetCurrentDirectory())).GetFiles("*.csproj", SearchOption.AllDirectories))       {         string fileName = Path.GetFileNameWithoutExtension(file.Name);                  if (seenElements.Add(fileName))         {           var guid = ReadGuid(file.FullName);           writer.WriteLine(string.Format(@"Project(""0"") = ""{0}"", ""{1}"",""{2}""", fileName, file.FullName, guid));           writer.WriteLine("EndProject");         }       }     }   }    static Guid ReadGuid(string fileName)   {     using (var file = File.OpenRead(fileName))     {       var elements = XElement.Load(XmlReader.Create(file));       return Guid.Parse(elements.Descendants().First(element => element.Name.LocalName == "ProjectGuid").Value);     }   } } 
like image 38
Bertrand Avatar answered Sep 21 '22 22:09

Bertrand