visual-studio 以编程方式生成 Visual Studio 解决方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2736260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:52:39  来源:igfitidea点击:

Programmatically generate Visual Studio Solution

c#.netvisual-studio

提问by Krishnaraj Barvathaya

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 in the project with their respective paths to fetch them and a list of project names. Is there any way I can achieve this

我想在 .net 中编写一个向导,它通过一些项目以编程方式生成 Visual Studio 解决方案。我将有一个 XML 文件,其中包含需要包含在项目中的文件的详细信息以及它们各自的获取路径和项目名称列表。有什么办法可以做到这一点

采纳答案by user344209

Well the Visual Studio SDK would be your path to go. There are methods where you can create files and I think also projects based on project templates. So for instance you can create a new "asp.net mvc" project or a class library. You could even create your own template.

好吧,Visual Studio SDK 将是您的必经之路。有一些方法可以创建文件,我认为还有基于项目模板的项目。例如,您可以创建一个新的“asp.net mvc”项目或类库。您甚至可以创建自己的模板。

I am quite sure that you can do this only inside VS.

我很确定您只能在 VS 中执行此操作。

Otherwise, there is nothing wrong to generate the project and sln files be generating the xml files, thats one of the good things that they are plain xml in the end. You just need to generate some Projectd Guids, but I think this would be fairly simple and totally "legal".

否则,生成项目和 sln 文件并生成 xml 文件没有错,这就是它们最终是纯 xml 的好处之一。您只需要生成一些 Projectd Guids,但我认为这将相当简单且完全“合法”。

good progress!

进展顺利!

回答by alastairs

Check out the Text Template Transformation Toolkit (T4). It's pretty sweet, and powers some of the file generation in ASP.NET MVC. Scott Hanselman's got a great introductory articleon the topic.

查看文本模板转换工具包 (T4)。它非常棒,并且为 ASP.NET MVC 中的一些文件生成提供了支持。Scott Hanselman 有一篇关于该主题的精彩介绍性文章

回答by CJBrew

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

您可以使用一小段代码或脚本来完成此操作。Visual Studio 将为您填写大部分 GUID...

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.

我把这样的事情放在一起作为一次性整理项目。它不是 100% 完美的,您最终可能会从代码处理项目名称的方式中得到重复。希望它会为您指明道路。

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.

我们在这里做的是设置解决方案文件的前导,然后插入每个解决方案(您需要项目类型的 guid,在此处看到以 FAE 开头,但不是项目自己的 GUID,VS 将在保存解决方案文件时插入)。有更多的样板,然后我们为每个项目插入构建配置。我为每个项目设置了大约 12 个配置(不同的 Release 和 Debug 设置),但我在此处将其压缩为两个。

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

回答by Bil Simser

We built a tool called Tree Surgeonthat does exactly this. Things are under review and discussion to revieve (or ditch it) as times have changed since it was originally created but it does what you're asking here (and is open source).

我们构建了一个名为Tree Surgeon的工具来完成此任务。事情正在和讨论中以重新审视(或放弃它),因为它最初创建以来已经发生了变化,但它可以满足您在此处的要求(并且是开源的)。