2

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?

4

1 回答 1

6

这是 Windows 上奇怪的命令行解析规则的结果。请参阅此处http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx。命令行实际上应该是:--folder "C:\my folder\\" --username john

这是用户输入错误。您不应该尝试更正它的代码;只是优雅地失败。

于 2013-01-21T15:58:33.300 回答