VB Command Line?

Gene Klamerus
Gene Klamerus used Ask the Experts™
on
I'm having trouble processing the VB Command line (or at least I'm confused).

My "test" code is:


Module Module1

    Sub Main()

        Dim NumArgs As Integer
        Dim Arguments As String()

        Arguments = Environment.CommandLine.Split

        NumArgs = Arguments.Count

        MsgBox(NumArgs.ToString)

        For Each argument In Arguments
            MsgBox(argument)
        Next

    End Sub

End Module


What I see is the count includes a "blank" entry.  The first entry shown is the name of the executable, and then a blank, and then the "real" arguments.  What's up with the blank?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
I'm not entirely sure about this behavior, but I have a feeling you are making this a bit more involved than it has to be  = )

Instead of splitting the CommandLine value, you can call GetCommandLineArgs to get the parameters passed to your application:

Arguments = Environment.GetCommandLineArgs()

Open in new window


...and that should give you what you seek. However, my personal preference would be to use the built in string array that you get with Main functions:

Sub Main(ByVal args() As String)

Open in new window


...and your code becomes:

Module Module1

    Sub Main(ByVal args() As String)
        MsgBox(args.Length)

        For Each argument In args
            MsgBox(argument)
        Next

    End Sub

End Module

Open in new window


Now with this approach, you don't get the command that was executed, but you could glean that from your previous logic and checking the first argument:

Dim command As String = Environment.GetCommandLineArgs()(0)

Open in new window


Just something for you to ponder  = D

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial