且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MSBuild脚本中获取当前目录?

更新时间:2023-02-21 15:25:14

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

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

然后MyProj.proj导入shared.targets,这是传递给msbuild.exe的那个,因此MSBuildProjectDirectory的值将始终为 C:\ temp ,即使您在内部引用该值也是如此.共享目标.如果您的shared.targets需要路径知识,则应在已知属性中声明它们.例如,C#项目文件定义OutputPath的值,而共享文件Microsoft.Common.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.

MSBuild 4

如果您使用的是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

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