Command lines need to be quoted; paths don't

Raymond Chen

While it’s true that one category of problems comes from failing to quote spaces in command lines, it is a false statement that “path names in the registry should have quotation marks around them for obvious reasons.” In fact it’s the opposite. Path names should not be quoted. Think about it: Quotation marks are a parsing issue, not a file name issue. The quotation marks are not part of the file name. If you type dir or open the folder in Explorer, you don’t see any quotation marks. You can’t pass quotation marks to CreateFile or GetFileAttributes since they aren’t actually part of the file name. Quotation marks indicate to code that is parsing a command line that a space should not be interpreted as the end of the command line token. Therefore, you need quotation marks when the string will be interpreted as a command line, such as in the lpCommandLine parameter to CreateProcess (but not the lpApplicationName parameter). Many places in the registry allow you to specify a command line, such as in the Run key or in the registration of static verbs. If the string will be used as a command line, then you need to quote the spaces so that the command line parser knows where the program name ends and the command line arguments begin. In the example in the linked comment, the registry key in question is a list of paths to files, not a list of command lines. Since it’s not a command line, quotation marks would result in an invalid file name, since quotation marks are not legal file name characters. The correct statement of the rule is that command lines in the registry should have quotation marks to protect spaces. Path names, on the other hand, not only do not require quotation marks, but in fact must not have quotation marks, because the quotation mark is not a part of the file name. Nitpicker’s corner

Observe that at no point did I claim that Microsoft employees are perfect. Microsoft employees make mistakes, too.


Discussion is closed.

Feedback usabilla icon