在C#Main方法中如何准确填充`string [] args`? [英] How exactly is `string[] args` populated in a C# Main method?

查看:55
本文介绍了在C#Main方法中如何准确填充`string [] args`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How exactly is string[] args populated in a C# Main method?

例如,是否去除了空白?是否有任何元素为空字符串或null?单引号和双引号如何处理?

For instance, is white space stripped? Are any of the elements ever empty string or null? How are single and double quotes handled?

MSDN不解释如何,而只是说

Main方法的参数是一个String数组,表示命令行参数

The parameter of the Main method is a String array that represents the command-line arguments

推荐答案

我相信给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.

这篇关于在C#Main方法中如何准确填充`string [] args`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆