本文介绍了如何找到通过* .csproject文件参考路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自动化的powershell脚本,它报告项目的引用和引用路径。
当.csproj中的hintpath没有填充时,我找不到一个方法来获取引用的路径。

I want to make an automated powershell script, that reports the references and the referencepaths of a project.When the hintpath in .csproj is not filled in, i can't find a way to get the path to the reference.

推荐答案

这里有一个快速的解决方案。它抓取当前目录下的每个 .csproj 文件,并检查每个引用。对于从GAC引用的程序集,只输出名称。对于GAC之外的程序集,将输出程序集的完整路径。

Here's a quick solution. It grabs every .csproj file under the current directory, and inspects each Reference. For assemblies referenced from the GAC, just the name is output. For assemblies outside the GAC, the full path to the assembly is output.

$projectFiles = get-childitem . *.csproj -Recurse 

foreach( $projectFile in $projectFiles )
{
    $projectXml = [xml] (get-content $projectFile.FullName)
    $projectDir = $projectFile.DirectoryName

    Write-Host "# $($projectFile.FullName) #"


    foreach( $itemGroup in $projectXml.Project.ItemGroup )
    {
        if( $itemGroup.Reference.Count -eq 0 )
        {
            continue
        }

        foreach( $reference in $itemGroup.Reference )
        {
            if( $reference.Include -eq $null )
            {
                continue
            }

            if( $reference.HintPath -eq $null )
            {
                Write-Host ("{0}" -f $reference.Include)
            }
            else
            {
                $fullpath = $reference.HintPath
                if(-not [System.IO.Path]::IsPathRooted( $fullpath ) )
                {
                    $fullPath = (join-path $projectDir $fullpath)
                    $fullPath = [System.IO.Path]::GetFullPath("$fullPath")
                }
                Write-Host $fullPath
            }
        }
    }

    Write-Host ''
}


$ b b

请注意,默认情况下,有一些注册表项MSBuild寻找,以找到没有提示路径的引用的位置。您可以看到MSBuild的外观以及它在哪里定位程序集通过编译与详细日志打开:

Note that by default, there are some registry entries that MSBuild looks in to find the locations of references that don't have hint paths. You can see where MSBuild looks and where it locates assemblies by compiling with verbose logging turned on:

msbuild My.csproj /t:build /v:d

这篇关于如何找到通过* .csproject文件参考路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 14:24