How can I get current directory in msbuild script?

Grz*_*nio 65 .net c# msbuild

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.

Say*_*imi 93

伊戈尔非常接近.MSBuildProjectDirectory是一个属性,它将为您提供在命令行上调用的项目文件的完整路径.因此,如果您有以下脚本:

  • C:\ TEMP\MyProj.proj
  • C:\共享\ shared.targets

并且MyProj.proj导入shared.targets这是传递给msbuild.exe的那个,那么即使你在shared.targets中引用了它,它的值MSBuildProjectDirectory也总是C:\ temp.如果您的shared.targets需要路径知识,那么应该在已知属性中声明它们.例如,C#项目文件定义值,OutputPath共享文件Microsoft.Common.targets使用该属性.

编辑:MSBuild 4

如果您使用的是MSBuild 4,则现在可以使用此类值的属性.

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

请参阅http://sedodream.com/2010/03/11/MSBuild40ReservedProperties.aspx.

  • +1请注意,即使支持这些新的MSBuild 4保留属性,也不会在Visual Studio 2010编辑对话框的宏列表中列出 (4认同)

gra*_*der 53

这里有3个有用的目标.

WhereAmI是我在尝试找出当前目录时使用的那个.其他的也是提供信息的.(有些超出了问题的范围).

<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>
Run Code Online (Sandbox Code Playgroud)

如果您正在使用"外部msbuild文件"并需要传递文件名或路径(因为外部msbuild文件不喜欢相关文件,如果它们与调用.msbuild文件不在同一目录中)....这是一个方便的(3.5及以上我相信)任务.

    <ConvertToAbsolutePath Paths="..\"> <!-- Some relative path here -->
      <Output TaskParameter="AbsolutePaths" PropertyName="MyAbsolutionPathProperty"/>
    </ConvertToAbsolutePath>            
    <Message Text="'MyAbsolutionPathProperty' = '$(MyAbsolutionPathProperty)'" />   
Run Code Online (Sandbox Code Playgroud)


Igo*_*hov 11

MSBuild有保留的属性MSBuildProjectDirectory,它是项目或脚本文件所在目录的绝对路径,在你的情况下是C:\ Dev.因此"$(MSBuildProjectDirectory)\temp",正是您正在寻找的.