tags:

views:

645

answers:

3

Hi,

In my msbuild script I need to pass the full directory as a parameter. How can get it?

Example: I am running the script from C:\dev, 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.

+1  A: 

MSBuild has reserved property called MSBuildProjectDirectory, which is to the absolute path of the directory where you project or script file is located, C:\Dev in your case. Therefore "$(MSBuildProjectDirectory)\temp" is exactly what you're looking for.

Igor Korkhov
+5  A: 

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 now use the properties for this type of value.

  • MSBuildThisFile
  • MSBuildThisFileDirectory
  • MSBuildThisFileDirectoryNoRoot
  • MSBuildThisFileExtension
  • MSBuildThisFileFullPath
  • MSBuildThisFileName

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

Sayed Ibrahim Hashimi
+5  A: 

Here are 3 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 beyond the scope of the question).

<Target Name="WhereAmI">
    <Message Text=" Here I Am  " />
    <Exec Command="dir ." />
    <Message Text=" " />
</Target>



<Target Name="ShowReservedProperties">  
    <Message Text=" MSBuildProjectDirectory  = $(MSBuildProjectDirectory)" />   
    <Message Text=" MSBuildProjectFile  = $(MSBuildProjectFile)" />     
    <Message Text=" MSBuildProjectExtension  = $(MSBuildProjectExtension)" />   
    <Message Text=" MSBuildProjectFullPath  = $(MSBuildProjectFullPath)" />     
    <Message Text=" MSBuildProjectName  = $(MSBuildProjectName)" />     
    <Message Text=" MSBuildBinPath  = $(MSBuildBinPath)" />     
    <Message Text=" MSBuildProjectDefaultTargets  = $(MSBuildProjectDefaultTargets)" />     
    <Message Text=" MSBuildExtensionsPath  = $(MSBuildExtensionsPath)" />   
    <Message Text=" MSBuildStartupDirectory  = $(MSBuildStartupDirectory)" />
</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>
granadaCoder