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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With