How can I get the current directory in an MSBuild script?

.net, c#, msbuild

Solution

Igor is pretty close. `MSBuildProjectDirectory` is the property that will give you the full path to the project file which was invoked on the command line. So if you have the following scripts:

- C:\temp\MyProj.proj

- C:\shared\shared.targets

And `MyProj.proj` imports `shared.targets` and this is the one passed to msbuild.exe then the value for `MSBuildProjectDirectory` will always be C:\temp even if you are referencing that inside of shared.targets. If your shared.targets requires path knowledge then those should be declared in known properties. For example C# project files define the value for `OutputPath` and the shared file `Microsoft.Common.targets` uses that property.

Edit: MSBuild 4

If you are using MSBuild 4, you can also use these properties for this type of value.

- MSBuildThisFile

- MSBuildThisFileDirectory

- MSBuildThisFileDirectoryNoRoot

- MSBuildThisFileExtension

- MSBuildThisFileFullPath

- MSBuildThisFileName

See http://sedodream.com/2010/03/11/MSBuild40ReservedProperties.aspx.

Problem

In my MSBuild script I need to pass the full directory as a parameter. How can I get it? Example: I am running the script from C:\dev, and I want a relative path, temp, so I am after C:\dev\temp. Note: I don't know from which folder the script will be run.

Original source