5

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
  • 1
  • Yes, exactly! I tried to read the link you provided, I didn't quite understand it though. I will read it some more. Thank you. Jul 17, 2013 at 20:55
  • The link explains you why you should not be doing it in the first place. If you must, however, you can run the WMI query provided with the .NET WMI tools.
    – GSerg
    Jul 17, 2013 at 20:57
  • I see, I will read it again. Actually, it is not a different process, it is the same process. Does that make a difference? Basically, if cmd is detected, I would like to get the path and arguments, not something like c:\windows\system32\cmd.exe. Jul 17, 2013 at 21:09

3 Answers 3

7

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
  • Thank you. But does this work for: (ByRef currentProcessList() As Process)? Jul 17, 2013 at 20:53
3

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 !

0

Another option

Sub WhatEver()
    Dim strArg() as string
    strArg = Command().Split(" ")
    ' strArg(0) is first argument and so on
    '
    '
End Sub
1
  • No good option to split at " ". If you call an exe like xxx.exe /Folder:"My New Folder" breaking at " " would destroy everything. You must also use the " char to hold parameters together! May 17, 2021 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.