Programmatically generate Visual Studio Solution

后端 未结 4 2132
执念已碎
执念已碎 2020-12-10 08:29

I want to write a wizard in .net which programmatically generates Visual Studio Solution with some projects. I will have a XML file with details of files need to be included

4条回答
  •  攒了一身酷
    2020-12-10 08:43

    You can do this with a short bit of code or script. Visual Studio will fill in most of the GUIDs for you...

    I knocked something like this together as a one-off to collate projects. It's not 100% perfect, you might end up with duplicates from the way the code handles project names. Hopefully it will show you the way.

    What we do here is set up the preamble to the Solution file, then insert each solution (you need the Project type guid, seen here beginning FAE, but not the Project's own GUID, which VS will insert on saving the Solution file). There's a bit more boilerplate then we insert the build configuration for each project. I had about 12 configurations for each project (different Release and Debug settings) but I've condensed it here to two.

    static void Main(string[] args)
         {
            if(args.Count() != 2)
            {
                Usage();
                return;
            }
    
            var rootDir = args[0];
            var output = args[1];
    
            var files = Directory.EnumerateFiles(rootDir, 
                                                 "*.*proj", 
                                                 SearchOption.AllDirectories);
    
            var configs = new StringBuilder();
    
            var configDefs = new string[]{
            ".Debug|Any CPU.ActiveCfg = Debug|Any CPU",
            ".Release|Any CPU.ActiveCfg = Release|Any CPU",
            "Other_configurations_see_solution_for_how"
            };
    
            using(var sw = new StreamWriter(output))
            {
                sw.WriteLine(Resources.Head);
                foreach(var file in files)
                {
                    var relpath = file.Substring(rootDir.Length + 1);
                    var split= relpath.Split('\\');
                    var name = split[0];
                    var path = relpath;
                    sw.WriteLine("Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{0}\", \"{1}\", \"{0}\"", name, path);
                    sw.WriteLine("EndProject");
    
                    foreach(var configDef in configDefs)
                    {
                        configs.AppendLine(string.Format("{0}{1}", file, configDef));
                    }
                }
    
                sw.WriteLine(@"Global
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Any CPU = Debug|Any CPU       
        Release|Any CPU = Release|Any CPU
        ...Other_configurations_see_solution_for_how...
    EndGlobalSection 
        GlobalSection(ProjectConfigurationPlatforms) = postSolution");
    
                sw.WriteLine(configs.ToString());
                sw.WriteLine(Resources.Tail);
            }
        }
    

    Head looks a bit like:

    Microsoft Visual Studio Solution File, Format Version 11.00
    # Visual Studio 2010
    

    But I think there are control characters in the first line - beware!

    Tail looks like

        GlobalSection(SolutionProperties) = preSolution
        HideSolutionNode = FALSE
        EndGlobalSection
    EndGlobal
    

提交回复
热议问题