Oliver Salzburg Oliver Salzburg - 9 days ago 4
C# Question

Spaces and backslashes in Visual Studio build events

I have an application that is supposed to aid my project in terms of pre- and post-build event handling. I'm using ndesk.options for command line argument parsing. Which gave me weird results when my project path contains spaces. I thought this was the fault of ndesk.options but I guess my own application is to blame. I call my application as a post-built event like so:

build.exe --in="$(ProjectDir)" --out="c:\out\"


A simple foreach over args[] displays the following:

--in=c:\my project" --out=c:\out"


What happened is that the last " in each parameter was treated as if it was escaped. Thus the trailing backslash was removed. And the whole thing is treated as a single argument.

Now I thought I was being smart by simply escaping the first " as well, like so:

build.exe --in=\"$(ProjectDir)" --out=\"c:\out\"


In that case the resulting args[] look like this:

--path="c:\my
project"
--out="c:\out"


The trailing backslash in the parameters is still swallowed and the first parameter is now split up.

Passing this args[] to ndesk.options will then yield wrong results.

How should the right command line look so that the correct elements end up in the correct args[] slots? Alternatively, how is one supposed to parse command line arguments like these with or without ndesk.options? Any suggestion is welcome.

Thanks in advance

Answer

Did you try to escape the last backslash?

build.exe --in="$(ProjectDir)\" --out="c:\out\\"

This works probably only, as long as the ProjectDir ends in \, which should be given.
This is just an idea, but I did not give it a try

EDIT:
I found a comment which suggests to leave out the trailing "