visual-studio 将多个现有 .csproj 添加到 Visual Studio 解决方案的简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1891035/
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
Easy way to add multiple existing .csproj to a Visual Studio Solution?
提问by Michael J Swart
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.
我已经从源代码管理中检出了 C# 代码的一个分支。它在各种文件夹中可能包含 50 个项目。找不到现有的 .sln 文件。
I intended to create a blank solution to add existing solutions. The UI only lets me do this one project at a time.
我打算创建一个空白解决方案来添加现有解决方案。UI 一次只允许我做一个项目。
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.
有什么我想念的吗?我想指定一个 *.csproj 文件列表,并以某种方式提出一个包含所有项目的 .sln 文件。
采纳答案by Michael Baker
Here is a PowerShell version of Bertrand's script which assumes a Src and Test directory next to the solution file.
这是 Bertrand 脚本的 PowerShell 版本,它假定解决方案文件旁边有一个 Src 和 Test 目录。
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()
回答by Mikkel Christensen
A PowerShell implementation that recursively scans the script directory for .csproj files and adds them to a (generated) All.sln:
一个 PowerShell 实现,它递归地扫描脚本目录中的 .csproj 文件并将它们添加到(生成的)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()
回答by Bertrand
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.
生成可执行文件的 AC# 实现,该实现创建了一个解决方案,其中包含执行它的目录和子目录中所有唯一的 *.csproj 文件。
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);
}
}
}
回答by kwesolowski
There is extension for VS available, capable of adding all projects in selected directory (and more):
有可用的 VS 扩展,能够添加选定目录中的所有项目(以及更多):
http://www.cyotek.com/blog/visual-studio-extension-for-adding-multiple-projects-to-a-solution
http://www.cyotek.com/blog/visual-studio-extension-for-adding-multiple-projects-to-a-solution
回答by Chris Fulstow
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.
您或许可以编写一个小的 PowerShell 脚本或 .NET 应用程序来解析所有项目的 .csproj XML 并提取它们的详细信息(ProjectGuid 等),然后将它们添加到 .sln 文件中。手动添加它们会更快,风险更小,但仍然是一个有趣的挑战。
回答by smaclell
Note:This is only for Visual Studio 2010
注意:这仅适用于 Visual Studio 2010
Found hereis 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.
这里找到了一个很酷的 Visual Studio 2010 插件,它在 VS 中为您提供了一个 PowerShell 控制台,让您可以与 IDE 进行交互。除了@Avram 提到的使用内置 VS 可扩展性可以做的许多其他事情之外,将文件或项目添加到解决方案非常容易。
回答by Marco Thomazini
Use Visual Studio Extension "Add Existing Projects". It works with Visual Studio 2012, 2013, 2015, 2017.
使用 Visual Studio 扩展“添加现有项目”。它适用于 Visual Studio 2012、2013、2015、2017。
To use the extension, open the Toolsmenu and choose Add Projects.
要使用该扩展程序,请打开“工具”菜单并选择“添加项目”。
回答by Gerardo Grignoli
Every answer seems to flatten the directory structure (all the projects are added to the solution root, without respecting the folder hierarchy). So, I coded my own console app that generates the solution and uses solution folders to group them.
每个答案似乎都使目录结构变平(所有项目都添加到解决方案根目录中,而不考虑文件夹层次结构)。因此,我编写了自己的控制台应用程序来生成解决方案并使用解决方案文件夹对它们进行分组。
Check out the project in GitHub
查看GitHub 中的项目
Usage
用法
SolutionGenerator.exe --folder C:\git\SomeSolutionRoot --output MySolutionFile.sln
回答by Asha
if you open the sln file with notepad you can see the format of the file which is easy to understand but for more info take a look @ Hack the Project and Solution Files.understanding the structure of the solution files you can write an application which will open all project files and write the application name ,address and GUID to the sln file .
如果您用记事本打开 sln 文件,您可以看到易于理解的文件格式,但有关更多信息,请查看@Hack the Project and Solution Files。了解解决方案文件的结构,您可以编写一个应用程序打开所有项目文件并将应用程序名称、地址和 GUID 写入 sln 文件。
of course I think if it's just once you better do it manually
当然我认为如果只是一次你最好手动完成
回答by Avram
Depends on visual studio version.
But the name of this process is "Automation and Extensibility for Visual Studio"
http://msdn.microsoft.com/en-us/library/t51cz75w.aspx
取决于视觉工作室版本。
但此过程的名称是“Visual Studio 的自动化和可扩展性”
http://msdn.microsoft.com/en-us/library/t51cz75w.aspx


