开发者

how to find reference path via *.csproject file

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开发者_StackOverflow中文版 to get the path to the reference.


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 ''
}

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


One (hacky) solution could be to make sure that all references have "Copy Local" set to true. That way, any dll referenced will always end up in the /bin directory, and thus you have a path to it.

If the intention is to have all referenced assemblies in one place, however, it may or may not make sense to copy even the references that exist in the GAC to a local copy instead, depending on the rest of your environment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜