Backslash treated as escape char in command line args

  • Thread starter Thread starter Philip Semanchuk
  • Start date Start date
P

Philip Semanchuk

Hi all,
Dotnet seems to be misparsing my command line args and I'd like to hear
about other people's experience with this. Everything is fine until I
mix backslashes and quotes in the args to my console app, e.g.:
myapp "c:\program files\" /foo /bar

Dotnet interprets the backslash preceding the quote as an escape
character with the result that args[0] comes through with the arguments
treated as one long string and the leading quote and second backslash
stripped, as follows:
c:\program files" /foo /bar

Other apps seem to handle such arguments as I expect. For instance, if I
start Word with this command line:
winword "c:\program files\"

Word responds with the error message that it can't open the file
C:\PROGRAM FILES\ which means it correctly interpreted the backslash as
a part of the path, and not as an escape character. Notepad behaves
similarly. In my opinion, this is a bug in Microsoft's Dotnet
implementation. Opinions appreciated.

I tried this under both C# and VB using both the args[] array passed in
through Main() and also with System.EnvironmentGetCommandLineArgs();
same results in all contexts. It'd be interesting to try this using Mono
under Linux where backslash isn't the path separator.

Thanks
Philip
 
Someone else posted about this last week, so I tried it out in VB .NET, C#,
and a Win32 C++ app. In all cases, backslashes in the command line were
treated like escape characters, meaning that you'd have to do your own
parsing. However, today I tried it again before replying to your post and
they all worked as they were supposed to. I have no idea why. The only thing
I can think of is that I autoinstall updates from Windows Update, so maybe
this was fixed in the past week.
 
Try like this

"C:\\Program Files\\Someexe.exe"

or
just put @ before your "" like this.
@"C:\Program Files\Someexe.exe"

HTH.
 
Ed said:
Someone else posted about this last week, so I tried it out in VB .NET, C#,
and a Win32 C++ app. In all cases, backslashes in the command line were
treated like escape characters, meaning that you'd have to do your own
parsing. However, today I tried it again before replying to your post and
they all worked as they were supposed to. I have no idea why. The only thing
I can think of is that I autoinstall updates from Windows Update, so maybe
this was fixed in the past week.

THanks, Ed, for the info and diagnosis.

Philip
 
Shailesh said:
Try like this

"C:\\Program Files\\Someexe.exe"

or
just put @ before your "" like this.
@"C:\Program Files\Someexe.exe"

Shailesh,
Thanks but my problem isn't with writing strings inside C# code. It has
to do with invoking a dotnet application from the command line, and how
those command line arguments get parsed incorrectly by the dotnet framework.

Philip
 
Back
Top