Get the arguments from a command line in vb.net

后端 未结 3 1782
轮回少年
轮回少年 2021-01-15 09:50

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

Dim processName As String

D         


        
相关标签:
3条回答
  • 2021-01-15 10:25

    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
    
    0 讨论(0)
  • 2021-01-15 10:27

    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 讨论(0)
  • 2021-01-15 10:38

    Another option

    Sub WhatEver()
        Dim strArg() as string
        strArg = Command().Split(" ")
        ' strArg(0) is first argument and so on
        '
        '
    End Sub
    
    0 讨论(0)
提交回复
热议问题