ClassMicrosoft.VisualBasic.Interaction SyntaxDim result As String = Command( ) DescriptionThe Command function returns the arguments used when launching an application created with Visual Basic. Usage at a Glance
ExampleThe 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 DifferencesVisual 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. |