最好的方法读取控制台应用程序的命令行参数 [英] Best way to read commandline parameters in console application

查看:280
本文介绍了最好的方法读取控制台应用程序的命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是阅读中的命令行参数的两种方式。第一是,我使习惯使用在主参数看到的方式。第二个我迷迷糊糊审查code时启动。我注意到,所述第二阵列的路径和应用程序,但在第一跳过这在分配的第一项。

Below are two ways of reading in the commandline parameters. The first is the way that I'm accustom to seeing using the parameter in the main. The second I stumbled on when reviewing code. I noticed that the second assigns the first item in the array to the path and application but the first skips this.

难道仅仅是preference或者是第二种方式,现在的好办法吗?

Is it just preference or is the second way the better way now?

Sub Main(ByVal args() As String)
    For i As Integer = 0 To args.Length - 1
        Console.WriteLine("Arg: " & i & " is " & args(i))
    Next

    Console.ReadKey()
End Sub





Sub Main()
    Dim args() As String = System.Environment.GetCommandLineArgs()

    For i As Integer = 0 To args.Length - 1
        Console.WriteLine("Arg: " & i & " is " & args(i))
    Next

    Console.ReadKey()
End Sub

我认为同样可以用C#来完成,所以它不一定是一个vb.net的问题。

I think the same can be done in C#, so it's not necessarily a vb.net question.

推荐答案

第二种方式是更好,因为它可以在外面的main()的使用,所以当你重构它少了一个要考虑的事情。

Second way is better because it can be used outside the main(), so when you refactor it's one less thing to think about.

此外,我不喜欢的魔术,来把ARGS的方法参数的第一种方式。

Also I don't like the "magic" that puts the args in the method parameter for the first way.

这篇关于最好的方法读取控制台应用程序的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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