将多个现有的.csproj添加到Visual Studio解决方案的简单方法? [英] Easy way to add multiple existing .csproj to a Visual Studio Solution?
问题描述
我已经从源代码管理中签出了C#代码的一个分支.它可能在各个文件夹中包含50个项目.找不到现有的.sln文件.
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.
我打算创建一个空白解决方案以添加现有解决方案. UI只允许我一次执行一个项目.
I intended to create a blank solution to add existing solutions. The UI only lets me do this one project at a time.
有什么我想念的吗?我想指定* .csproj文件的列表,并以某种方式提出一个包含所有项目的.sln文件.
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.
推荐答案
此处是Bertrand脚本的PowerShell版本,在解决方案文件旁边假定有Src和Test目录.
Here is a PowerShell version of Bertrand's script which assumes a Src and Test directory next to the solution file.
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()
这篇关于将多个现有的.csproj添加到Visual Studio解决方案的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!