【问题标题】:find all VS-projects that compile to a .NET 3.5 console application查找所有编译为 .NET 3.5 控制台应用程序的 VS 项目
【发布时间】:2020-07-04 08:50:09
【问题描述】:

我有一个深度嵌套的存储库,其中包含许多 Visual Studio 项目。我需要找到所有具有 TargetFramework 3.5Output Type 'console application' 的项目。

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

<OutputType>Exe</OutputType>

带有窗口的应用程序有这个:

<OutputType>WinExe</OutputType>

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

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

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

感谢任何帮助。

【问题讨论】:

    标签: visual-studio powershell select-string


    【解决方案1】:

    您可以利用Select-String 接受多个 搜索模式的能力,然后您可以使用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,了解无法做出此假设的情况的解决方案。

    【讨论】:

      【解决方案2】:

      您可以将 $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 类型的新集合。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多