Command line arguments, paths with spaces, parsing incorrectly

后端 未结 3 1871
被撕碎了的回忆
被撕碎了的回忆 2021-01-25 02:22

I have an issue that\'s nearly identical to the one outlined here (Console app not parsing correctly args with white spaces) but that answer has no bearing on my situation.

3条回答
  •  再見小時候
    2021-01-25 02:28

    It is caused by the \" at the end of your first argument.

    If you remove the trailing slash or add an extra slash before the quote then you will get two arguments. The quote is being escaped and is considered part of the first argument until it hits the next quote 2 characters later. After that you are no longer inside of a quote and thus each space results in another argument added to the args array.

    This is caused by the fact that .Net uses CommandLineToArgvW to process command line arguments and the rules it uses to handle escaping of characters.

    Jon Galloway has a writeup on this

    To quote:

    CommandLineToArgvW has a special interpretation of backslash characters when they are followed by a quotation mark character ("), as follows:

    • 2n backslashes followed by a quotation mark produce n backslashes followed by a quotation mark.
    • (2n) + 1 backslashes followed by a quotation mark again produce n backslashes followed by a quotation mark.
    • n backslashes not followed by a quotation mark simply produce n backslashes.

提交回复
热议问题