开发者

Know if a program launched from command line versus clicking on the .exe in vb.net?

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
      开发者_如何学Python'Do some events
    Else
      'Do some other events
    End If

End Sub

Thanks for your help.


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.


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.


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.


Well this is old, but for anyone reading this, I find that the easiest way is to simple check if there is more that 1 argument.

If arg.Count > 1 Then
    'Code
End If

The first argument will always be the name of the applocation you are launching.


This should work:

If Console.LargestWindowHeight = 0 Then
   'Forms application
Else
   'Command line application
End If


You Should add a boolean and do this

Public AllowAppRunV As Boolean = Nothing
Public Sub CheckForCommandLine()
    For Each arg As String In My.Application.CommandLineArgs

        Select Case Trim(LCase(arg))
            Case "/allowrun"
                MessageBox.Show("allow")
                AllowAppRunV = True
            Case Else

                MessageBox.Show("else")

        End Select

    Next

    If AllowAppRunV = True Then

    Else
        MessageBox.Show("Close")
    End If
End Sub

i know its a very long time but maybe someone will find this useful

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜