简体   繁体   中英

in a console app what's the best way to prevent special sequences in command line arguments from being escaped?

I wrote this console app that reads a sequence of command line arguments and does something with them.

The problem is that if the user enters something like:

 --folder "C:\my folder\" --username john

the args String array of the Main function will have 2 elements rather than 4:

1st element: "--folder"
2nd element: "C:\my folder\" --username john"

(the \\" sequence is escaped as a double quote.)

Since not using quotes would result in 5 elements...

--folder C:\my folder\ --username john

1st element: --folder
2nd element: C:\my
3rd element: folder\
4th element: --username
5th element: john

... what's the best way to get around this problem?

This is a consequence of the weird command line parsing rules on Windows. See here http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx . The command line should actually be: --folder "C:\\my folder\\\\" --username john

This is a user input error. You shouldn't try to correct it code; just fail gracefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM