Get parent directory in MSBuild

In NAnt, I have a very simple property to get the root of my project, it looks like this:

<property name="project.root.folder" value="${directory::get-parent-directory(directory::get-parent-directory(project.local.folder))}" /> 

This brings me to the root of my project, from which I build all my paths.

In MSBuild, I can use $(MSBuildProjectDirectory) to get my current directory, but I would like to get the full path to the parent directory. NAnt uses directory::get-parent-directory which works charmingly and I hope something like this is available in MSBuild.

I found a previous similar question ( https://stackoverflow.com/a/312947/ ), but I think there should be something more accessible, of course!

Sam :)

+8
source share
2 answers

I assume this is MSBuild 4.0. You can do it:

 <PropertyGroup> <RootFolder>$([System.IO.Path]::GetDirectoryName($(MSBuildProjectDirectory)))</RootFolder> </PropertyGroup> <Message Text="RootFolder: '$(RootFolder)'" /> 
+14
source

Your question has its own answer, and it looks decent. MSBuild is built around projects, not solutions, so finding something to give you a solution path requires a bit of customization. One fact to keep in mind is that for many projects, decision files are not located at the root of the project tree (or โ€œconeโ€ in MSBuild).

Reserved MSBuild Properties

0
source

All Articles