Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 606
Polite Stranger Avatar asked Feb 16 '23 21:02

Polite Stranger


2 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
like image 139
George Johnston Avatar answered Feb 18 '23 10:02

George Johnston


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 !

like image 39
schlebe Avatar answered Feb 18 '23 11:02

schlebe