Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何从一组现有项目创建解决方案文件(.sln)?_.net_Visual Studio - Fatal编程技术网

.net 如何从一组现有项目创建解决方案文件(.sln)?

.net 如何从一组现有项目创建解决方案文件(.sln)?,.net,visual-studio,.net,Visual Studio,有没有办法为大量项目快速创建.sln文件 向一个解决方案中添加200多个项目是相当繁琐的,我正在寻找一个能够遍历目录树并使我能够从不同位置一次添加多个项目的解决方案 我真的在寻找一个漂亮的GUI来实现这一点。我认为没有工具可以实现这一点,但解决方案文件格式非常简单,所以自己动手很容易: static void Main() { string header = @" Microsoft Visual Studio Solution File, Format Version 12.00 #

有没有办法为大量项目快速创建.sln文件

向一个解决方案中添加200多个项目是相当繁琐的,我正在寻找一个能够遍历目录树并使我能够从不同位置一次添加多个项目的解决方案


我真的在寻找一个漂亮的GUI来实现这一点。

我认为没有工具可以实现这一点,但解决方案文件格式非常简单,所以自己动手很容易:

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);
    }
}
(您无需生成
ProjectConfigurationPlatforms
部分,打开解决方案时,Visual Studio将自动为您创建默认生成配置)


上面的代码只处理C#项目;如果您有其他项目类型,您可能需要调整它(您需要为每个项目类型找到合适的GUID)。

关闭投票人:IDE问题是关于StackOverflow的主题,根据您是否真的需要在一个解决方案中包含200个项目?如果我是你,我会把它分成几个解决方案(可能有些项目属于几个或所有解决方案)@ThomasLevesque应用程序的设计有助于有很多项目,并且随着时间的推移,这些项目会有机地增长。我正在寻找一种快速的方法来创建解决方案文件,将这些文件分成不同的部分以便于使用。不幸的是,Visual Studio使这项任务非常艰巨,因此尚未完成,而且我没有一个包含所有项目的大型.sln。请参阅