如何将带空格的路径和尾部反斜杠作为属性传递给MSBuild

Dmi*_*nov 8 msbuild properties

当我尝试将一些目录路径传递到MSBuild脚本时,如下所示:

MSBuild.exe myproj.proj /p:DirPath="C:\this\is\directory\"
Run Code Online (Sandbox Code Playgroud)

在.proj文件中我用它作为

<PropertyGroup>
  <FilePath>$(DirPath)file.txt</FilePath>
<PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

然后MSBuild编写FilePathc:\this\is\directory"file.txt.如果我传递DirPath没有引号但是使用尾部斜杠(/p:DirPath=c:\this\is\directory\)或没有尾部斜杠但是带引号(/p:DirPath="c:\this\is\directory\")然后一切正常.

允许通过尾部斜杠传递目录路径(它会更方便)和引号(因为路径可以包含空格)可以做些什么?或者它是MSBuild中的一个错误,我应该使用一些解决方法,比如在将它传递给msbuild时删除尾部反斜杠?

Sof*_*ter 9

这是因为在命令行上设置属性的方式.MSBuild正在添加"由于最后一个'\'而在值的末尾,因此"被附加到字符串路径的末尾.

从命令行设置值时添加一个额外的\,字符串将按预期为您添加反斜杠,或者不将"放在最后.

MSBuild.exe myproj.proj /p:DirPath="C:\this\is\directory\\"
Run Code Online (Sandbox Code Playgroud)

那么值是:C:\ this\is\directory\file.txt

另一个问题是您可以将此功能放在MSBuild项目中以替换":

<PropertyGroup>
    <DirPath>$(DirPath.Replace('"',""))</DirPath>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)