Reading command line arguments

RenanLazarotto 0 Tallied Votes 1K Views Share

I've found this a little hard to find, so I decided to share with you guys.

This small code will allow your app to accept arguments.

Just copy the whole code into the main sub (I didn't tested elsewhere) and edit the code inside the for to do what you want. Voila! It works.

Hope it helps!

Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs

For i As Integer = 0 To CommandLineArgs.Count - 1
'the message box is just a example. you can make Ifs to check the arguments and do the desired options
MessageBox.Show(CommandLineArgs(i))
Next

Dani AI

Generated

Nice find, — and good follow-ups from and . A few practical clarifications that help avoid surprises:

My.Application.CommandLineArgs returns a ReadOnlyCollection(Of String) containing only the arguments (it does not include the executable path) and is the comfortable, VB-friendly way to read args in many desktop apps. It is not the right source for ClickOnce deployments, and for single-instance apps subsequent launches deliver their args via the StartupNextInstance event instead. (learn.microsoft.com)
The legacy Command()/Interaction.Command approach returns the raw argument string; it still works but My.Application.CommandLineArgs or Environment APIs are generally preferable. (learn.microsoft.com)

If you want predictable parsing anywhere in the app, use Environment.GetCommandLineArgs() (or receive args in Sub Main(args As String())). Note GetCommandLineArgs() returns a string array whose element 0 is the executable path, so skip index 0 when processing options. Here is a small VB.NET pattern you can adapt for switches and quoted values:

' VB.NET: simple switch parsing using Environment.GetCommandLineArgs
Sub Main()
    Dim argv() As String = Environment.GetCommandLineArgs()
    For i As Integer = 1 To argv.Length - 1
        Dim a As String = argv(i).Trim()
        If a.StartsWith("/port:", StringComparison.OrdinalIgnoreCase) Then
            Dim portStr = a.Substring(6)
            Dim port As Integer
            If Integer.TryParse(portStr, port) Then
                ' use port
            End If
        ElseIf a.StartsWith("--file=", StringComparison.OrdinalIgnoreCase) Then
            Dim filePath = a.Substring(7).Trim(""""c)
            ' use filePath
        Else
            ' positional or unknown arg
        End If
    Next
End Sub

For debugging, set your arguments in Project -> Properties -> Debug (Visual Studio 2022 shows an "Open debug launch profiles UI" link) or in Properties/launchSettings.json using commandLineArgs. Always quote arguments that contain spaces and validate/parsing defensively in production. (learn.microsoft.com)

codeorder 197 Nearly a Posting Virtuoso

Also, check out this link for a more defined explanation to using "Command Line Arguments".

anoopkh 0 Newbie Poster

The command line argument can be shown in a messagebox like the following

msgbox(command())

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.