Easy way to add multiple existing .csproj to a Vis

2019-01-13 03:09发布

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.

13条回答
ゆ 、 Hurt°
2楼-- · 2019-01-13 04:04

Check this out: http://nprove.codeplex.com/

It is a free addin for vs2010 that does that and more if the projects are under the tfs

查看更多
放荡不羁爱自由
3楼-- · 2019-01-13 04:05

Use Visual Studio Extension "Add Existing Projects". It works with Visual Studio 2012, 2013, 2015, 2017.

enter image description here

To use the extension, open the Tools menu and choose Add Projects.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-13 04:06

Building on Bertrand's answer at https://stackoverflow.com/a/16069782/492 - make a console app out of this and run it in the root folder where you want the VS 2015 Solution to appear. It works for C# & VB (hey! be nice).

It overwrites anything existing but you source control, right?

Check a recently used .SLN file to see what the first few writer.WriteLine() header lines should actually be by the time you read this.

Don't worry about the project type GUID Ptoject("0") - Visual Studio will work that out and write it in when you save the .sln file.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace AddAllProjectsToNewSolution
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("starting");
            using (var writer = new StreamWriter("AllProjects.sln", false, Encoding.UTF8))
            {
                writer.WriteLine("Microsoft Visual Studio Solution File, Format Version 14.00");
                writer.WriteLine("# Visual Studio 14");
                writer.WriteLine("VisualStudioVersion = 14.0.25420.1");
                var seenElements = new HashSet<string>();

                foreach (var file in (new DirectoryInfo(Directory.GetCurrentDirectory())).GetFiles("*.*proj", SearchOption.AllDirectories))
                {
                    string extension = file.Extension;
                    if (extension != ".csproj" && extension != ".vbproj")
                    {
                        Console.WriteLine($"ignored {file.Name}");
                        continue;
                    }

                    Console.WriteLine($"adding {file.Name}");

                    string fileName = Path.GetFileNameWithoutExtension(file.Name);

                    if (seenElements.Add(fileName))
                    {
                        var guid = ReadGuid(file.FullName);
                        writer.WriteLine($"Project(\"0\") = \"{fileName}\", \"{GetRelativePath(file.FullName)} \", \"{{{guid}}}\"" );
                        writer.WriteLine("EndProject");
                    }

                } 
            }
             Console.WriteLine("Created AllProjects.sln. Any key to close");
             Console.ReadLine();
        }

        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);
            }
        }
        // https://stackoverflow.com/a/703292/492
        static string GetRelativePath(string filespec, string folder = null)
        {
            if (folder == null)
                folder = Environment.CurrentDirectory;

            Uri pathUri = new Uri(filespec);
            // Folders must end in a slash
            if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString()))
                folder += Path.DirectorySeparatorChar;

            Uri folderUri = new Uri(folder);
            return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace('/', Path.DirectorySeparatorChar));
        }
    }
}
查看更多
We Are One
5楼-- · 2019-01-13 04:07

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);
    }
  }
}
查看更多
够拽才男人
6楼-- · 2019-01-13 04:07

You might be able to write a little PowerShell script or .NET app that parses all the projects' .csproj XML and extracts their details (ProjectGuid etc.) then adds them into the .sln file. It'd be quicker and less risky to add them all by hand, but an interesting challenge nonetheless.

查看更多
虎瘦雄心在
7楼-- · 2019-01-13 04:10

Note: This is only for Visual Studio 2010

Found here is a cool add in for Visual Studio 2010 that gives you a PowerShell console in VS to let you interact with the IDE. Among many other things you can do using the built in VS extensibility as mentioned by @Avram, it would be pretty easy to add files or projects to a solution.

查看更多
登录 后发表回答