Get the Arguments from a Command Line in Vb. Net

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

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
4

3 Answers

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

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

Another option

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

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.

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 args is C/C++/C#/Java technic.

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.

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 args technic that is more linked to C and Unix where command syntax accepts arguments separated by space !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest