3

Is there a way to create a .sln file quickly for a large number of projects?

Its rather tedious to add 200+ projects to a solution, and I am looking for a solution that traverse a directory tree and give me the ability to add multiple projects all at once from difference locations.

I am really looking for a nice looking GUI that will do this.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
Aaron M
  • 2,523
  • 1
  • 23
  • 38
  • Close voters: IDE questions are on topic for StackOverflow as per http://meta.stackexchange.com/questions/20251/where-to-ask-a-question-about-an-ide – Billy ONeal Jan 22 '14 at 21:37
  • 1
    Do you really need 200 projects in one solution? If I were you, I'd split it into several solutions (with perhaps some projects that belong to several or all solutions) – Thomas Levesque Jan 22 '14 at 21:41
  • @ThomasLevesque The design of the application facilitates having a lot of projects and its grown organically over time. I am looking for a quick way to create Solution files that split those up into different slices for easy consumption, unfortunately, Visual Studio makes this task arduous, so it hasn't been done yet, and I don't have one massive .sln that has all the projects in it. – Aaron M Jan 22 '14 at 21:50
  • see https://github.com/chris1248/SolutionBuilder – C.J. May 08 '17 at 03:54

1 Answers1

2

I don't think there's a tool to do that, but the solution file format is quite simple, so it's pretty easy to do it yourself:

static void Main()
{
    string header = @"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1";

    string globalSections = @"Global
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Any CPU = Debug|Any CPU
        Release|Any CPU = Release|Any CPU
    EndGlobalSection
    GlobalSection(SolutionProperties) = preSolution
        HideSolutionNode = FALSE
    EndGlobalSection
EndGlobal";

    string csharpProjectTemplate = @"Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{0}"", ""{1}"", ""{2}""
EndProject";

    string rootPath = @"D:\MyProjects";
    string solutionName = "MySolution";
    string solutionPath = Path.Combine(rootPath, solutionName + ".sln");
    var projectFiles = Directory.GetFiles(rootPath, "*.csproj", SearchOption.AllDirectories);
    using (var stream = File.OpenWrite(solutionPath))
    using (var writer = new StreamWriter(stream))
    {
        writer.WriteLine(header);
        var xmlns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
        foreach (var projectFile in projectFiles)
        {
            string name = Path.GetFileNameWithoutExtension(projectFile);
            string relativePath = projectFile.Substring(rootPath.Length).TrimStart('\\');
            var doc = XDocument.Load(projectFile);
            var guidElement = doc.Root.Elements(xmlns + "PropertyGroup")
                                      .Elements(xmlns + "ProjectGuid")
                                      .FirstOrDefault();
            if (guidElement == null)
                continue;

            string guid = guidElement.Value;

            string entry = string.Format(csharpProjectTemplate, name, relativePath, guid);
            writer.WriteLine(entry);
        }
        writer.WriteLine(globalSections);
    }
}

(You don't need to generate the ProjectConfigurationPlatforms section, Visual Studio will automatically create the default build configurations for you when you open the solution)

The code above only handles C# projects; you might need to adapt it if you have other project types (you'll need to find the appropriate GUID for each project type).

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758