Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

msbuild

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.

like image 998
Grzenio Avatar asked Jan 21 '10 17:01

Grzenio


People also ask

Where is MSBuildExtensionsPath defined?

This file is installed by Visual Studio to the standard $(MSBuildExtensionsPath) location (C:\Program Files\MSBuild).

What is MSBuild script?

MSBuild is the build platform for Microsoft and Visual Studio. This walkthrough introduces you to the building blocks of MSBuild and shows you how to write, manipulate, and debug MSBuild projects. You will learn about: Creating and manipulating a project file.

How do I use MSBuild EXE?

Use MSBuild at a command prompt To run MSBuild at a command prompt, pass a project file to MSBuild.exe, together with the appropriate command-line options. Command-line options let you set properties, execute specific targets, and set other options that control the build process.


2 Answers

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.

like image 111
Sayed Ibrahim Hashimi Avatar answered Oct 04 '22 11:10

Sayed Ibrahim Hashimi


Here are three targets that are helpful.

WhereAmI is the one I use when trying to figure out my current directory of course.

The others are informative as well (some are beyond the scope of the question).

<Target Name="WhereAmI">     <Message Text=" Here I Am  " />     <Exec Command="dir ." />     <Message Text=" " /> </Target>    <Target Name="ShowReservedProperties" AfterTargets="BeforeBuild">     <Message Text=" MSBuildProjectDirectory  = $(MSBuildProjectDirectory)" Importance="high" />     <Message Text=" MSBuildProjectFile  = $(MSBuildProjectFile)" Importance="high" />     <Message Text=" MSBuildProjectExtension  = $(MSBuildProjectExtension)" Importance="high" />     <Message Text=" MSBuildProjectFullPath  = $(MSBuildProjectFullPath)" Importance="high" />     <Message Text=" MSBuildProjectName  = $(MSBuildProjectName)" Importance="high" />     <Message Text=" MSBuildBinPath  = $(MSBuildBinPath)" Importance="high" />     <Message Text=" MSBuildProjectDefaultTargets  = $(MSBuildProjectDefaultTargets)" Importance="high" />     <Message Text=" MSBuildExtensionsPath  = $(MSBuildExtensionsPath)" Importance="high" />     <Message Text=" MSBuildStartupDirectory  = $(MSBuildStartupDirectory)" Importance="high"/> </Target>     <Target Name="ShowOtherProperties">     <Message Text="  " />     <Message Text="  " />     <Message Text=" Environment (SET) Variables*       " />     <Message Text=" ---------------------------        " />     <Message Text=" COMPUTERNAME = *$(COMPUTERNAME)*   " />     <Message Text=" USERDNSDOMAIN = *$(USERDNSDOMAIN)* " />     <Message Text=" USERDOMAIN = *$(USERDOMAIN)*       " />     <Message Text=" USERNAME = *$(USERNAME)*           " /> </Target> 

If you're using an "external MSBuild file" and need to pass a filename or path to it (because external MSBuild files do not like relative files if they are not in the same directory as the calling .msbuild file)....here is a convenient (3.5 and up I believe) Task.

    <ConvertToAbsolutePath Paths="..\"> <!-- Some relative path here -->       <Output TaskParameter="AbsolutePaths" PropertyName="MyAbsolutionPathProperty"/>     </ConvertToAbsolutePath>     <Message Text="'MyAbsolutionPathProperty' = '$(MyAbsolutionPathProperty)'" /> 
like image 35
granadaCoder Avatar answered Oct 04 '22 12:10

granadaCoder