C# 如何在 MSBuild 脚本中获取当前目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2111256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the current directory in an MSBuild script?
提问by Grzenio
In my MSBuild script I need to pass the full directory as a parameter. How can I get it?
在我的 MSBuild 脚本中,我需要将完整目录作为参数传递。我怎么才能得到它?
Example: I am running the script from C:\dev, and I want a relative path temp, so I am after C:\dev\temp.
示例:我从 C:\dev 运行脚本,我想要一个相对路径 temp,所以我在 C:\dev\temp 之后。
Note: I don't know from which folder the script will be run.
注意:我不知道脚本将从哪个文件夹运行。
采纳答案by Sayed Ibrahim Hashimi
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:
伊戈尔非常接近。MSBuildProjectDirectory
是为您提供在命令行上调用的项目文件的完整路径的属性。因此,如果您有以下脚本:
- C:\temp\MyProj.proj
- C:\shared\shared.targets
- 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:\tempeven 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.
和MyProj.proj
导入shared.targets
,这是传递给 msbuild.exe 的那个,那么即使您在 shared.targets 中引用它,其值MSBuildProjectDirectory
也将始终为C:\temp。如果您的 shared.targets 需要路径知识,那么这些应该在已知属性中声明。例如,C# 项目文件定义值,OutputPath
共享文件Microsoft.Common.targets
使用该属性。
Edit: MSBuild 4
编辑:MSBuild 4
If you are using MSBuild 4, you can also use these properties for this type of value.
如果您使用的是 MSBuild 4,您还可以将这些属性用于此类值。
- MSBuildThisFile
- MSBuildThisFileDirectory
- MSBuildThisFileDirectoryNoRoot
- MSBuildThisFileExtension
- MSBuildThisFileFullPath
- MSBuildThisFileName
- MSBuildThisFile
- MSBuildThisFile目录
- MSBuildThisFileDirectoryNoRoot
- MSBuildThis 文件扩展名
- MSBuildThisFileFullPath
- MSBuildThis 文件名
See http://sedodream.com/2010/03/11/MSBuild40ReservedProperties.aspx.
请参阅http://sedodream.com/2010/03/11/MSBuild40ReservedProperties.aspx。
回答by Igor Korkhov
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.
MSBuild 保留了名为 的属性MSBuildProjectDirectory
,它是您的项目或脚本文件所在目录的绝对路径,在您的情况下为 C:\Dev。因此,"$(MSBuildProjectDirectory)\temp"
这正是您要寻找的。
回答by granadaCoder
Here are 3 Targets that are helpful.
这里有 3 个有用的目标。
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).
当然,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>
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.
如果您使用的是“外部 msbuild 文件”并且需要将文件名或路径传递给它(因为外部 msbuild 文件不喜欢相对文件,如果它们与调用 .msbuild 文件不在同一目录中)...这是一个方便的(我相信 3.5 及更高版本)任务。
<ConvertToAbsolutePath Paths="..\"> <!-- Some relative path here -->
<Output TaskParameter="AbsolutePaths" PropertyName="MyAbsolutionPathProperty"/>
</ConvertToAbsolutePath>
<Message Text="'MyAbsolutionPathProperty' = '$(MyAbsolutionPathProperty)'" />