How to find the reference path through the * .csproject file

I want to make an automatic powershell script that reports the links and link paths of a project. When the hintpath in .csproj is not full, I cannot find a way to get the link path.

+4
source share
2 answers

Here is a quick fix. It captures every .csproj file in the current directory and checks each link. For assemblies referenced by the GAC, only the name is displayed. For assemblies outside the GAC, the full assembly path is displayed.

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

Please note that by default there are some registry entries that MSBuild searches to find link locations that do not have hint paths. You can see where MSBuild looks and where it finds assemblies by compiling them with detailed logging:

 msbuild My.csproj /t:build /v:d 
+2
source

One (hacker) solution might be to make sure all Copy Local links are set to true. That way, any link to the dll will always be in the / bin directory, and therefore you have a path to it.

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

0
source

Source: https://habr.com/ru/post/1343793/


All Articles