从 vb.net 中的命令行获取参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17709561/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:00:17  来源:igfitidea点击:

Get the arguments from a command line in vb.net

vb.netcommand-line

提问by Polite Stranger

Is it possible to return the arguments from the processPathin this example?
This might make more sense, sorry.

processPath在这个例子中是否可以从 返回参数?
这可能更有意义,抱歉。

Dim processName As String

Dim processPath As String

If processName = "cmd" Then
    Dim arguments As String() = Environment.GetCommandLineArgs()
    Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments))
End If

回答by George Johnston

A simple (and clean) way to accomplish this would be to just modify your Sub Mainas follows,

完成此操作的一种简单(且干净)的方法是Sub Main按如下方式修改您的内容,

Sub Main(args As String())
   ' CMD Arguments are contained in the args variable
   Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", args))
End Sub

回答by Joop

Another option

另外一个选项

Sub WhatEver()
    Dim strArg() as string
    strArg = Command().Split(" ")
    ' strArg(0) is first argument and so on
    '
    '
End Sub

回答by schlebe

The VB.Net solution to your problem is to use Command()VB.Net function when you search to display command's line of currently executed process.

VB.Net 解决您的问题的方法是Command()在您搜索以显示当前执行的进程的命令行时使用VB.Net 函数。

Sub Main(args As String())
     Dim sCmdLine As String = Environment.CommandLine()
     Console.WriteLine("CommandLine: " & sCmdLine)
     Dim iPos = sCmdLine.IndexOf("""", 2)
     Dim sCmdLineArgs = sCmdLine.Substring(iPos + 1).Trim()
     Console.WriteLine("CommandLine.Arguments: " & sCmdLineArgs)
End Sub

The first outpout will display the complete command's line with name of program.

第一个输出将显示带有程序名称的完整命令行。

The second output will display only the command's line without program's name.

第二个输出将只显示没有程序名称的命令行。

Using argsis C/C++/C#/Java technic.

使用的args是 C/C++/C#/Java 技术。

Using CommandLine()function is pure VB and is more intuitive because is return command's line as typed by user without supposing that arguments are type without blank.

使用CommandLine()函数是纯 VB 并且更直观,因为返回命令的行是由用户键入的,而不假设参数是没有空格的类型。

Example:

例子:

LIST-LINE 1-12, WHERE=(20-24='TYPES'),to-area=4
LIST-LINE 1 - 12, WHERE = ( 20-24 = 'TYPES' ) , to-area = 4

In this command's syntax, arguments are separated by COMMA and not by spaces.

在此命令的语法中,参数由逗号而不是空格分隔。

In this case, it is better to not use argstechnic that is more linked to C and Unix where command syntax accepts arguments separated by space !

在这种情况下,最好不要使用args与 C 和 Unix 更相关的技术,其中命令语法接受以空格分隔的参数!