Trailing backslash in C# command-line parameter: Bug or feature?

Viewed 60

When I give a path like c:\some folder with a blank into the Debug option of Visual Studio I need to escape it with a double quote " just like you would on the command line as well.

If I do it like that "c:\some folder with a blank" then the args[0] parameter is "c:\\some folder with a blank"

However, if I do it like this "c:\some folder with a blank\" then the args[0]actually contains: "c:\\some folder with a blank\"", having the final " as an actual part of the string.

The same is true for when I call it from the command line directly.

1 Answers

The \" is an escape sequence for ". If you want the last backslash then use the escape sequence for the backslash \\

Command line argument:

"c:\some folder with a blank\\"

And because you have last " as an escape sequence, the argument is read till the end. If you omit " at the end, you still have the whole argument.

Try these arguments yourself

"c:\some folder with a blank\

"c:\some folder \" with a blank\
Related