查找编译为 .NET 3.5 控制台应用程序的所有 VS 项目

find all VS-projects that compile to a .NET 3.5 console application

我有一个深度嵌套的存储库,其中有很多 Visual Studio 个项目。我需要找到所有具有 TargetFramework 3.5 输出类型 'console application'.

的项目

我发现控制台应用程序在它们的 csproj 文件中有这个:

<OutputType>Exe</OutputType>

带有 window 的应用程序有这个:

<OutputType>WinExe</OutputType>

这让我可以找到所有扩展名为 csproj 的文件,这些文件可以编译为控制台应用程序。

$csproj_ConsoleExes = gci -Recurse *.csproj | select-string "<OutputType>Exe</OutputType>"

接下来我需要做什么:我需要只过滤那些使用 target-framework 3.5 的项目。但是由于 $csproj_ConsoleExes 包含搜索结果,我不知道如何再次应用 select-stringselect-string 仅适用于 FileInfo.

类型的输入对象

感谢任何帮助。

您可以将 $csproj_ConsoleExes 中的项目转换为键入 FileInfo

$csproj_Console_Items = $csproj_ConsoleExes| select Path | get-item

上面的行首先获取每个项目的路径并将其通过管道传递给 get-item,然后将其转换为 FileInfo-object。

然后你可以找到所有包含 TargetFrameworkVersion

的行
$csproj_Console_TargetFrameworkVersion=$csproj_Console_Items | select-string "<TargetFrameworkVersion>"

现在您可以再次获取路径并将其通过管道传输到 get-item 以获取 FileInfo 类型的新集合。

您可以利用 Select-String's ability to accept multiple search patterns, which allows you to then use Group-Object 来确定所有 模式匹配的文件:

Get-ChildItem -Recurse -Filter *.csproj |
  Select-String -SimpleMatch '<OutputType>Exe</OutputType>',
                             '<TargetFramework>net35</TargetFramework>' | 
    Group-Object Path |
      Where-Object Count -eq 2 | 
        ForEach-Object Name

以上输出所有 *.csproj 个文件的完整路径,其中 两个 模式都被发现。

注:

  • 这种方法只有在每个搜索模式最多 一个 行每个输入文件匹配时才有效,这对于 .csproj 应该是正确的文件。

  • 有关无法做出此假设的情况的解决方案,请参阅 this answer