Command Function


Command Function

Class

Microsoft.VisualBasic.Interaction

Syntax

     Dim result As String = Command(  ) 

Description

The Command function returns the arguments used when launching an application created with Visual Basic.

Usage at a Glance

  • Command returns a string containing everything entered on the command line after the executable filename. If there are no arguments available to return, the function returns an empty string.

  • Command returns an unparsed string containing the command-line arguments. Use the Split function to break the argument list up by spaces for easier processing.

         Dim eachArgument(  ) As String     eachArgument = Split(Command(  ), " ") 

  • The shared GetCommandLineArgs method of the System.Environment class returns a string array with a first element that is the program filename, and with remaining elements that are the command-line arguments.

  • During the development phase, you can pass arguments to your program using the Command line arguments text box, found in the Debug section of the application's Property Pages.

Example

The following example parses the command-line arguments, reporting them to the console. It looks for a minus (-) or slash (/) sign at the start of an argument and considers everything after a colon to be additional information for the argument. Given the command-line arguments:

     -d:50 -f -g -k 

the program outputs the following text to the console:

     Option d, with parameter = 50     Option f     Option g     Option k 

The source code is as follows:

     ' Uses: Imports MVB = Microsoft.VisualBasic     Public Sub ParseCommandLine(  )        ' ----- Parse and display each argument.        Dim eachArgument(  ) As String        Dim counter As Integer        Dim optionText As String        Dim paramText As String        Dim colonPos As Integer        ' ----- Put the arguments in an array.        eachArgument = Split(Command(  ), " ")        For counter = 0 To eachArgument.Length - 1           optionText = eachArgument(counter)           If (MVB.Left(optionText, 1) = "-") Or _                 (MVB.Left(optionText, 1) = "/") Then              ' ----- Found a valid argument. Remove the - or /.              optionText = Mid(optionText, 2)              ' ----- See if there is a parameter.              paramText = ""              colonPos = InStr(optionText, ":")              If (colonPos > 0) Then                 paramText = Mid(optionText, colonPos + 1)                 optionText = MVB.Left(optionText, colonPos - 1)              End If              ' ----- Report on the option.              If (paramText = "") Then                 Console.WriteLine("Option " & optionText)              Else                 Console.WriteLine("Option " & optionText & _                    ", with parameter = " & paramText)              End If           End If        Next counter     End Sub 

Version Differences

Visual Basic 2005 includes a new My.Application.CommandLineArgs property that returns a collection of the command-line arguments, with each argument as a separate item in the collection.




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net