views:

57

answers:

3

Hi I'm writing a program in vb.net. The program can be started from another program by passing some arguments or it can be lauched by clicking .exe. I'd like to show the user some options depending on where he is coming. Is the below approach correct?

Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Environment.GetCommandLineArgs(0).ToString = "SomeArgument" Then
      'Do some events
    Else
      'Do some other events
    End If

End Sub

Thanks for your help.

+1  A: 

It looks like you're on the right track, however you ought to be checking for the argument after the first position. From the GetCommandLineArgs documentation:

The first element in the array contains the file name of the executing program. If the file name is not available, the first element is equal to String.Empty. The remaining elements contain any additional tokens entered on the command line.

Your code is checking the first element of the array so it would be the program's name. Depending on how many arguments you expect to pass in you should loop through it and determine whether it exists.

For Each arg As String In Environment.GetCommandLineArgs()
    If arg = "SomeArg" Then
        ' do something
    End If
Next

' LINQ approach
If Environment.GetCommandLineArgs().Any(Function(arg) arg = "SomeArg") Then
    ' do something
End If

Also, it's a string array so there's no need to use ToString() on the element.

Ahmad Mageed
Thank you for your answer.
Kral24
A: 

Unless I don't understand what you want, you should iterate over the CommandLine Args collection and only prompt for the ones not provided. This will solve the problem of the user starting your app from the commandline w/out any arguments. This way, you only ever prompt for the arguments that aren't explicitly passed on the commandline.

Like this:

Dim someArgument as String = String.Empty
Dim myArgument as String = String.Empty


For Each arg as String In Environment.GetCommandLineArgs()
    If arg.StartsWith("SomeArgument") Then
        someArgument = arg
    End If
    If arg.StartsWith("MyArgument") Then
        myArgument = arg
    End If
    ' Continue for each extra argument
Next


If String.IsNullOrEmpty(someArgument) Then
    ' prompt for someArgument
End If

If String.IsNullOrEmpty(myArgument) Then
    ' prompt for myArgument
End If

The only tricky part here, is parsing out the value of "arg" in the for loop, since it will be something like "SomeArgument=someValue". My code doesn't split them out, you'll likely want to do that.

Nate Bross
Thank you. it looks like I was on the right path.
Kral24
A: 

It is sort of correct. If the user double-clicks the EXE, then Windows will launch your program with no command-line arguments. On the other hand, you can't stop the user launching your app from the command line with no arguments either. Nor can you stop the user manually typing on the command line the same parameters that your calling program passes.

One thing that is wrong with your current code is that the first element (index 0) in the Environment.GetCommandLineArgs() array will actually be your program name (the name of the EXE file). You can get around this by checking for index 1, but if there are no additional arguments then this will throw an IndexOutOfBoundsException. So you need to check the length of the array before indexing into it.

itowlson