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.
To add existing projects to the solution: Right click the Solution node in Solution Explorer select Add -> Multiple Projects.
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.
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.
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()
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); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With