Command, Command$
返回用于启动程序的命令行参数部分。
语法:
- Command$()
- Command()
带$后缀的形式返回String;不带后缀的形式返回Variant(String)。
对于编译为可执行文件的应用程序,Command返回命令行中应用程序名称之后出现的任何参数。例如,命令行:
MyApp /switch arg1 arg2Command返回"/switch arg1 arg2"。
示例
本示例使用Command检索命令行参数并将其拆分为数组。
vb
Function GetCommandLine(Optional MaxArgs As Variant) As Variant
Dim Ch As String, CmdLine As String, CmdLnLen As Long
Dim InArg As Boolean, I As Long, NumArgs As Long
If IsMissing(MaxArgs) Then MaxArgs = 10
ReDim ArgArray(MaxArgs)
NumArgs = 0
InArg = False
CmdLine = Command()
CmdLnLen = Len(CmdLine)
For I = 1 To CmdLnLen
Ch = Mid(CmdLine, I, 1)
If Ch <> " " And Ch <> vbTab Then
If Not InArg Then
If NumArgs = MaxArgs Then Exit For
NumArgs = NumArgs + 1
InArg = True
End If
ArgArray(NumArgs) = ArgArray(NumArgs) & Ch
Else
InArg = False
End If
Next I
ReDim Preserve ArgArray(NumArgs)
GetCommandLine = ArgArray()
End Function