vb.net 如何在VB.NET中获取Main方法的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12779446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get arguments of Main method in VB.NET
提问by James
In C# Console Application, Main method has a string() argument, but in VB.NET, the Main method doesn't contain the argument, if I set command line arguments, how to retrieve them?
在 C# 控制台应用程序中,Main 方法有一个 string() 参数,但在 VB.NET 中,Main 方法不包含该参数,如果我设置了命令行参数,如何检索它们?
In C#, if I right click the project and select Properties, set command line arguments in debug item, I can use args[0], args[1] and so on to achive my arguments. In VB.NET, I found
在 C# 中,如果我右键单击项目并选择属性,在调试项中设置命令行参数,我可以使用 args[0]、args[1] 等来实现我的参数。在VB.NET中,我发现
System.Environment.GetCommandLineArgs()
can also achive them, but it contains one more argument, it also contains the path of the process, anyone can help?
也可以实现它们,但它包含一个更多的参数,它还包含进程的路径,任何人都可以帮忙吗?
回答by Guffa
You can just add the arguments to the Main
methoid, and it works just like in C#:
您可以将参数添加到Main
方法中,它的工作方式就像在 C# 中一样:
Sub Main(args As String())
回答by Grant Thomas
You could add them to the entry point method signature (ByVal args() As String
) and access them just as you would in C#. You could also use VB.NETs My
class to access them such as My.Application.CommandLineArgs
.
您可以将它们添加到入口点方法签名 ( ByVal args() As String
) 并像在 C# 中一样访问它们。您还可以使用 VB.NETsMy
类来访问它们,例如My.Application.CommandLineArgs
.
回答by D. Barton
and then you can use this code snippet to actually make use of the command-line arguments. Not asked or needed by the original poster but provided for those who run across this post. This snippet shows how to see if there are command-line arguments and how to access them:
然后您可以使用此代码片段来实际使用命令行参数。原始海报没有询问或需要,但提供给那些跑过这篇文章的人。此代码段显示了如何查看是否有命令行参数以及如何访问它们:
Dim myFilename As String
If args.Length > 0 Then
myFilename = args(0)
End If
...