Home > Software design >  Is there a way to reference the root development folder in MSBuild or Visual Studio?
Is there a way to reference the root development folder in MSBuild or Visual Studio?

Time:09-25

Visual Studio has wonderful macros (terrible name for a variable) for things like $SolutionDir and $ProjectDir but I cannot find anything that points to my root development folder. I understand that this is a nebulous concept in some cases but it would sure be helpful to have a way to reference my root .git checkout folder so I don't have to use relative paths that break when things get moved around. Is anyone aware of a way to derive this in Visual Studio that I might be missing? I could write an extension for it but I'd ideally like a means that works in Visual Studio and MSBuild (for Azure DevOps) so I don't have to develop two solutions.

CodePudding user response:

You could do something like this using property functions:

<PropertyGroup><ProjectRootDir>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), '.git/index'))</ProjectRootDir></PropertyGroup>

This should work from within any level of directory inside your folder structure (that is inside any MSBuild file therein), as long as you only have .git\index once in your folder structure (at your definition of "top most" level).

  • Related