msbuilddirectoryparenttargets

Get parent directory of MSBuildProjectDirectory


I have solution with projects. Also I have in solution directory folder with msbuild files.

In msbuild file I have next code :

<PropertyGroup Label="Build Directories">
   <RootPath>$([System.IO.Path]::GetFullPath('$(MSBuildProjectDirectory)'))</RootPath>
</PropertyGroup>

<ItemGroup>
   <MSBuildProjectInfrastructure Include="$(RootPath)MyApp.Services.Infrastructure.sln">
   <AdditionalProperties>Configuration=$(Configuration);Platform=$(Platform);</AdditionalProperties>
   </MSBuildProjectInfrastructure>
 </ItemGroup>

Which works badly, as I need to go in parent directory to find MyApp.Services.Infrastructure.sln

Structure :

SolutionFolder

 -- MsBuildsFolder

 -- ProjectFile

Here is quite similar question, but doesn't resolve my issue


Solution

  • To get the parent folder, you can let MSBuild determine the location of known file in that folder by means of the built in property function GetDirectoryNameOfFileAbove:

      <PropertyGroup>
        <RootPath>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), MyApp.Services.Infrastructure.sln))</RootPath>
      </PropertyGroup>