Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 to get the path to the reference.

like image 783
woutvdd Avatar asked Oct 24 '22 17:10

woutvdd


1 Answers

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
like image 64
Aaron Jensen Avatar answered Oct 27 '22 10:10

Aaron Jensen