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

How can I get the current directory in an MSBuild script?(如何在 MSBuild 脚本中获取当前目录?)
本文介绍了如何在 MSBuild 脚本中获取当前目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在我的 MSBuild 脚本中,我需要将完整目录作为参数传递.我怎么才能得到它?

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

示例:我从 C:dev 运行脚本,我想要一个相对路径 temp,所以我在 C:dev emp.

Example: I am running the script from C:dev, and I want a relative path, temp, so I am after C:dev emp.

注意:我不知道脚本将从哪个文件夹运行.

Note: I don't know from which folder the script will be run.

推荐答案

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: empMyProj.proj
  • C:sharedshared.targets

MyProj.proj 导入 shared.targets 并且这是传递给 msbuild.exe 的那个,那么 MSBuildProjectDirectory 的值将始终为C: emp 即使您在 shared.targets 中引用它.如果您的 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: emp 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.

这篇关于如何在 MSBuild 脚本中获取当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

MSBuild cannot find a reference(MSBuild 找不到参考)
The reference assemblies for framework .NETCore, Version=v5.0 were not found(未找到框架 .NETCore,Version=v5.0 的参考程序集)
quot;File has a different computed hash than specified in manifestquot; error when signing the EXE(“文件的计算哈希值与清单中指定的不同签署EXE时出错)
Good-practices: How to reuse .csproj and .sln files to create your MSBuild script for CI?(良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 MSBuild 脚本?)
Run an MSBuild target only if project is actually built(仅在实际构建项目时运行 MSBuild 目标)
MS-Build BeforeBuild not firing(MS-Build BeforeBuild 未触发)