如何列出 Visual Studio 内给定解决方案的所有项目的所有目标框架?

jul*_*gon 14 solution visual-studio target-framework

给定 Visual Studio 中打开的解决方案,如何快速检查解决方案中的各个项目具有哪些目标框架?是否有一个解决方案范围的视图显示每个项目针对哪个目标框架,或者有多少项目针对每个框架版本的聚合视图?

我知道我可以单独检查每个项目(在属性窗口或文件csproj本身上),但是在具有 100 多个项目的解决方案中这是不可行的。

此外,我知道我可能可以csproj在根文件夹中的文件内进行某种正则表达式搜索,但我想知道 Visual Studio 中是否有内置的东西可以提供此数据。

Bre*_*tin 1

我找不到任何东西,所以决定编写一个脚本:

# Set root folder to current script location
$rootFolder = $PSScriptRoot
$solutionName = '[YOUR_SOLUTION_PATH]'

# Define the path to the solution file
$solutionFile = Join-Path $rootFolder $solutionName

# Read the contents of the solution file
$solutionText = Get-Content $solutionFile

# Use a regular expression to extract the names of the project files
$projectFiles = [regex]::Matches($solutionText, 'Project\("{([A-Za-z0-9-]+)}"\) = "([^"]+)", "([^"]+.csproj)"') | ForEach-Object { $_.Groups[3].Value } | Sort-Object

# Define project collection
$projects = @()

# Iterate over each project file
foreach ($projectFile in $projectFiles) {

    # Read the contents of the project file
    $projectText = Get-Content (Join-Path $rootFolder $projectFile)

    # Determine whether it is a SDK style project
    $isSdkProject = [regex]::IsMatch($projectText, '<Project Sdk="Microsoft.NET.Sdk">')

    # Use a regular expression to extract the target framework
    $targetFramework = [regex]::Match($projectText, '<TargetFramework>(.+)</TargetFramework>')
    
    # Get the target framework
    $foundFramework = if ($targetFramework.Success) { $($targetFramework.Groups[1].Value) } else { 'None' }

    # Add to projects collection
    $projects += [pscustomobject]@{ Project=$projectFile; SdkFormat=$isSdkProject; TargetFramework=$foundFramework; }
}

# Output projects as table
$projects | Format-Table

# Display summary
Write-Host $projects.Count "projects found"
Run Code Online (Sandbox Code Playgroud)

它列出了所有项目、它们的目标框架以及它们是否是 SDK 风格。