如何使用dotnet sln将所有项目添加到单个解决方案中?

时间:2018-08-25 12:28:38

标签: c# .net projects-and-solutions solution dotnet-cli

以下here中的示例正在尝试执行

dotnet sln AllProjects.sln add **/*.csproj

但我收到此错误:

  

找不到项目或目录**/*.csproj

看起来像通配符不起作用。我在做什么错了?

3 个答案:

答案 0 :(得分:4)

对于 Windows,打开 PowerShell 并运行此命令将所有项目添加到解决方案文件中:

 dotnet sln add (ls -r **/*.csproj)

答案 1 :(得分:3)

我错过了这个声明:

  

基于Unix / Linux的终端支持球形模式

我的Windows PowerShell解决方案如下:

$projects = Get-ChildItem -Recurse | Where-Object { $_.Name -match '^.+\.(csproj|vbproj)$' }

$uniqueProjects = $projects | Group-Object -Property Name | Where Count -EQ 1 | select -ExpandProperty Group | % { $_.FullName }

Invoke-Expression -Command "dotnet new sln -n AllProjects"

$uniqueProjects | % { Invoke-Expression -Command "dotnet sln AllProjects.sln add ""$_""" }

答案 2 :(得分:3)

在Windows上,您还可以使用以下命令将子目录中的所有项目递归添加到预先存在的解决方案文件中:

FOR /R %i IN (*.csproj) DO dotnet sln add "%i"

或者,如果您需要经常(重新)创建解决方案文件,则可以创建具有以下内容的批处理文件,然后在需要时就运行它:

dotnet new sln 
FOR /R %%i IN (*.csproj) DO dotnet sln add "%%i"

请注意,在批处理文件中尝试执行此操作时需要额外的%。