How to pass path with spaces and backslash in MSBuild as a property

When I try to pass the directory path in an MSBuild script as follows:

MSBuild.exe myproj.proj /p:DirPath="C:\this\is\directory\" 

And in the .proj file I use it as

 <PropertyGroup> <FilePath>$(DirPath)file.txt</FilePath> <PropertyGroup> 

Then MSBuild compiles FilePath as c:\this\is\directory"file.txt . If I pass DirPath without quotes but with a trailing slash ( /p:DirPath=c:\this\is\directory\ ) or without a trailing slash traits, but with quotes ( /p:DirPath="c:\this\is\directory\" ), everything works fine.

What can be done to allow traversal of a directory path with a trailing slash (this would be more convenient) and in quotation marks (since the path may contain spaces)? Or is this a bug in MSBuild and should I use some workaround, like removing backslash when passing it to msbuild?

+7
source share
1 answer

This is due to how the property is set on the command line. MSBuild adds "to the end of the value due to the last" \ "and therefore" is added to the end of the string path.

Add an additional parameter \ when setting the value from the command line, and the line will add a backslash for you as intended or will not put "at the end".

 MSBuild.exe myproj.proj /p:DirPath="C:\this\is\directory\\" 

Value: C: \ this \ is \ directory \ file.txt

Another thought is that you can put this function in your MSBuild project to replace ":

 <PropertyGroup> <DirPath>$(DirPath.Replace('"',""))</DirPath> </PropertyGroup> 
+9
source

All Articles