且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#Main方法中如何准确填充`string [] args`?

更新时间:2023-11-18 15:40:52

我相信给Main的args是

I believe the args given to Main are those returned by Environment.GetCommandLineArgs() after removing the first of the list. MSDN describes the suprisingly complex logic concerning backslashes:

命令行参数由空格分隔.您可以使用双引号()在参数中包含空格.但是,单引号(')不提供此功能.

Command line arguments are delimited by spaces. You can use double quotation marks (") to include spaces within an argument. The single quotation mark ('), however, does not provide this functionality.

如果双引号后面有两个或偶数个反斜杠,则将每个前进的反斜杠对替换为一个反斜杠,并删除双引号.如果双引号后跟奇数个反斜杠,包括一个反斜杠,则前面的每对反斜杠都将替换为一个反斜杠,并删除其余的反斜杠;但是,在这种情况下,双引号不会被删除.

If a double quotation mark follows two or an even number of backslashes, each proceeding backslash pair is replaced with one backslash and the double quotation mark is removed. If a double quotation mark follows an odd number of backslashes, including just one, each preceding pair is replaced with one backslash and the remaining backslash is removed; however, in this case the double quotation mark is not removed.

在评论中感谢Christian.K.

Thanks to Christian.K in the comments.