Łatwy sposób na dodanie wielu istniejących.csproj do rozwiązania Visual Studio?

Sprawdziłem gałąź kodu C# z kontroli źródeł. Zawiera około 50 projektów w różnych folderach. Nie istnieje .plik sln do znalezienia.

Zamierzałem stworzyć puste rozwiązanie, aby dodać istniejące rozwiązania. Interfejs pozwala mi robić tylko ten jeden projekt na raz.

Czy coś mi umyka? Chciałbym podać listę *.pliki csproj i jakoś wymyślić .plik sln zawierający wszystkie projekty.

Author: p.campbell, 2009-12-12

13 answers

Oto wersja PowerShell skryptu Bertranda, która zakłada Katalog Src I test obok pliku rozwiązania.

function GetGuidFromProject([string]$fileName) {
    $content = Get-Content $fileName

    $xml = [xml]$content
    $obj = $xml.Project.PropertyGroup.ProjectGuid

    return [Guid]$obj[0]
}

$slnPath = "C:\Project\Foo.sln"

$solutionDirectory = [System.IO.Path]::GetDirectoryName($slnPath)

$srcPath = [System.IO.Path]::GetDirectoryName($slnPath)
$writer = new-object System.IO.StreamWriter ($slnPath, $false, [System.Text.Encoding]::UTF8)

$writer.WriteLine("Microsoft Visual Studio Solution File, Format Version 12.00")
$writer.WriteLine("# Visual Studio 2013")

$projects = gci $srcPath -Filter *.csproj -Recurse

foreach ($project in $projects) {
   $fileName = [System.IO.Path]::GetFileNameWithoutExtension($project)

   $guid = GetGuidFromProject $project.FullName

   $slnRelativePath = $project.FullName.Replace($solutionDirectory, "").TrimStart("\")

   # Assume the project is a C# project {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
   $writer.WriteLine("Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""$fileName"", ""$slnRelativePath"",""{$($guid.ToString().ToUpper())}""")
   $writer.WriteLine("EndProject")
}

$writer.Flush()
$writer.Close()
$writer.Dispose()
 2
Author: Michael Baker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-07 02:05:41

Implementacja PowerShell, która rekurencyjnie skanuje katalog skryptów w poszukiwaniu .pliki csproj i dodaje je do (wygenerowanego) All.sln:

$scriptDirectory = (Get-Item $MyInvocation.MyCommand.Path).Directory.FullName
$dteObj = [System.Activator]::CreateInstance([System.Type]::GetTypeFromProgId("VisualStudio.DTE.12.0"))

$slnDir = ".\"
$slnName = "All"

$dteObj.Solution.Create($scriptDirectory, $slnName)
(ls . -Recurse *.csproj) | % { $dteObj.Solution.AddFromFile($_.FullName, $false) }

$dteObj.Solution.SaveAs( (Join-Path $scriptDirectory 'All.sln') ) 

$dteObj.Quit()
 21
Author: Mikkel Christensen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-19 02:24:40

Implementacja C#, która tworzy plik wykonywalny, który tworzy rozwiązanie zawierające wszystkie unikalne*.pliki csproj z katalogu i podkatalogów, w których jest wykonywany.

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);
    }
  }
}
 15
Author: Bertrand,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-10 09:23:20

Dostępne jest rozszerzenie dla VS, umożliwiające dodanie wszystkich projektów w wybranym katalogu (i nie tylko):

Http://www.cyotek.com/blog/visual-studio-extension-for-adding-multiple-projects-to-a-solution

 8
Author: kwesolowski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-01 21:24:17

Być może będziesz w stanie napisać mały skrypt PowerShell lub aplikację .NET, która przetwarza wszystkie projekty".csproj XML i wyciąga ich szczegóły (ProjectGuid itp.) następnie dodaje je do .plik sln. Byłoby szybciej i mniej ryzykowne, aby dodać je wszystkie ręcznie, ale mimo to interesujące wyzwanie.

 4
Author: Chris Fulstow,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-11 21:54:36
 3
Author: smaclell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-24 17:10:42

Jeśli otworzysz plik sln za pomocą Notatnika, zobaczysz format pliku, który jest łatwy do zrozumienia, ale po więcej informacji zajrzyj do @ Hack plików projektu i rozwiązań .zrozumienie struktury plików rozwiązań można napisać aplikację, która otworzy wszystkie pliki projektu i zapisze nazwę aplikacji, adres i GUID do pliku sln .

Oczywiście myślę, że jeśli to tylko raz, lepiej zrób to ręcznie

 1
Author: Asha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-11 23:30:23

Każda odpowiedź wydaje się spłaszczyć strukturę katalogów (wszystkie projekty są dodawane do katalogu głównego rozwiązania, bez poszanowania hierarchii katalogów). Zakodowałem więc własną aplikację konsolową, która generuje rozwiązanie i grupuje je za pomocą folderów rozwiązań.

Zobacz projekt w GitHub

Użycie

  SolutionGenerator.exe --folder C:\git\SomeSolutionRoot --output MySolutionFile.sln
 1
Author: Gerardo Grignoli,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-24 18:23:00

Zależy od wersji visual studio.
Ale nazwa tego procesu to "automatyzacja i rozszerzalność dla Visual Studio"
http://msdn.microsoft.com/en-us/library/t51cz75w.aspx

 0
Author: Avram,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-11 22:06:46

Zobacz też: http://nprove.codeplex.com/

Jest to darmowy dodatek do vs2010, który robi to i więcej jeśli projekty są w ramach tfs

 0
Author: Jose Luis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-12 07:57:00

Bazując na odpowiedzi Bertranda na https://stackoverflow.com/a/16069782/492 - Utwórz z tego aplikację konsolową i uruchom ją w folderze głównym, w którym ma pojawić się rozwiązanie VS 2015. Działa na C# i VB (Hej! bądź miły).

Nadpisuje wszystko, co istnieje, ale ty kontrolujesz źródło, prawda?

Sprawdź ostatnio używane .Plik SLN, aby zobaczyć, jakie powinny być pierwsze kilka linii nagłówka writer.WriteLine(), zanim to przeczytasz.

Nie martw się o typ projektu GUID Ptoject("0") - Visual Studio opracuje to i zapisze po zapisaniu .plik sln.

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));
        }
    }
}
 0
Author: CAD bloke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:31

Użyj Rozszerzenia Visual Studio " Dodaj Istniejące Projekty ". Współpracuje z Visual Studio 2012, 2013, 2015, 2017.

Tutaj wpisz opis obrazka

Aby użyć rozszerzenia, otwórz menu Narzędzia i wybierz Dodaj projekty.

 0
Author: Marco Thomazini,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-24 21:27:44

Jeśli wybierzesz "Pokaż wszystkie pliki" w Eksploratorze rozwiązań, możesz wyświetlić wszystkie pliki i folery, wybrać je i kliknąć prawym przyciskiem myszy, aby dodać je za pomocą "Dołącz w projekcie".

 -2
Author: Gabriël,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-29 14:15:54