Check Command Line Args RRS feed

  • Question

  • Hi,

    I use this command to check if a specific command line is used:

    If My.Application.CommandLineArgs.Contains...

    The problem is that cannot use ToLower or ToUpper so I have to repeat the check for all the combinations:

    Sample, SAMPLE, sample, SampleTwo, etc...

    Any advise to use it another way?!


    Thursday, October 24, 2019 3:32 PM


All replies

  • How about the way it is used in the example code at ConsoleApplicationBase.CommandLineArgs Property?

    • Marked as answer by OSVBNET Friday, October 25, 2019 1:18 PM
    Thursday, October 24, 2019 3:49 PM
  • Hi

    Would this work for you?

    Dim myTest As String = "SaMpLe"
    Dim comms As ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs
    For Each s As String In comms
    	If s.ToLower = myTest.ToLower Then
    		' got it (EQUALS)
    	End If
    	' OR ...............
    	If s.ToLower.Contains(myTest.ToLower) Then
    		' got it (CONTAINS)
    	End If


    Regards Les, Livingston, Scotland

    • Edited by leshay Thursday, October 24, 2019 3:57 PM
    Thursday, October 24, 2019 3:54 PM
  • If you are simply looking for an occurrence of "sample" then contains method will work.

    Under project properties, Application tab, click "View Application events"

    Replace with 

    Imports System.Collections.ObjectModel
    Namespace My
        Partial Friend Class MyApplication
            Private _Sample As Boolean
            Public ReadOnly Property HasSample() As Boolean
                    Return _Sample
                End Get
            End Property
            Protected Overrides Function Oninitialize(
                commandLineArgs As ReadOnlyCollection(Of String)) As Boolean
                _Sample = commandLineArgs.Cast(Of String).
                    Select(Function(arg) arg.ToLower()).Contains("sample")
                Return MyBase.OnInitialize(commandLineArgs)
            End Function
        End Class
    End Namespace

    Usage say in a form/button click event or in a class method.

    If My.Application.HasSample Then
    End If

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Thursday, October 24, 2019 5:28 PM
  • Hi,

    Have you solved this problem now?
    I think the above reply can provide you with a solution, have you tried it?
    If so, hope you can mark the reply as answer as this will help others looking for the same or similar issues down the road.

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 25, 2019 10:00 AM
  • How about the way it is used in the example code at ConsoleApplicationBase.CommandLineArgs Property?

    Thanks all, BTW this was easier:

    For Each s As String In My.Application.CommandLineArgs
        If s.ToLower.Contains("/blah") Then...
    Friday, October 25, 2019 1:18 PM