Environment.GetCommandLineArgs () in the StartupNextInstance event

I am having trouble trying to get Environment.GetCommandLineArgs()to work in an event the StartupNextInstanceway it happens in a form event Load. The code below checks to see if the application received command line arguments and sends the path to a function FileOpen()that basically opens the file in my program by entering the file name in its parameters.

If Environment.GetCommandLineArgs().Length > 1 Then FileOpen(Environment.GetCommandLineArgs(1))

The code above works fine in the event Load, although it doesn't work in the event StartupNextInstance. I also tried the code below to get the path to the args command line file:

Private Sub MyApplication_StartupNextInstance(sender As Object, e As ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
    Dim strFile As String = Environment.CommandLine
    If strFile.Length > 0 Then frmMain.FileOpen(strFile)
End Sub

The problem with the above code is that it does not receive the path to the file, but does not receive the file that was used to open the program (using the Open with ... method when right-clicking on the file) it will open the location of the EXE program file .

When I tried e.CommandLine, I received an error message:

A value of type 'System.Collections.ObjectModel.ReadOnlyCollection (Of String)' cannot be converted to 'String'.

+4
source share
1 answer

You can handle your application's StartupNextInstance event and use the parameter e.CommandLineto get a list of all newly accepted arguments.

Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
    If e.CommandLine.Count > 0 Then frmMain.FileOpen(e.CommandLine(0))
End Sub

In addition Environment.GetCommandLineArgs(), e.CommandLinedoes not contain the application executable path as the first element.

+3
source

All Articles