1

I have an application that can read certain types of files, and I have it working so that if you do "open with" from windows, it automatically starts the application and opens the selected file.

Unfortnately, I cannot get it to work for more than one file.

System.Environment.GetCommandLineArgs() contrains the following: System.Environment.GetCommandLineArgs(0) = name and path to the .exe System.Environment.GetCommandLineArgs(1) = name and path to the first file selected to be opened

System.Environment.GetCommandLineArgs().Length is 2 when the user tries to open 1 file, which makes sense since the first argument is the .exe itself and the 2nd is the path to the file, but it does not increase to 3 if the user tries to open 2 files... meaning that System.Environment.GetCommandLineArgs(2) is never populated

Here is some sample code that shows the problem: It will recognize no files or 1 file being opened, but if you try to open multiple it will only show the first.

    Private Sub Form_Main_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.Show()

    ' Check if the user is opening a file upon startup
    If System.Environment.GetCommandLineArgs().Length > 1 Then
        Dim i As Integer


        'this outputs the exe path and the the first file, if it exists, but never the 2nd file...
        'For i = 0 To System.Environment.GetCommandLineArgs().Length - 1
        '    MsgBox(System.Environment.GetCommandLineArgs(i))
        'Next

        'this outputs the first file, if it exists, but never the 2nd file...
        For i = 1 To System.Environment.GetCommandLineArgs().Length - 1
            MsgBox(System.Environment.GetCommandLineArgs(i))
        Next


    End If

End Sub

Is there something I am missing? Is there an alternative to using System.Environment.GetCommandLineArgs()

Also, i noticed that i CAN indeed have multiple command arguments if i specify them in the shortcut to the .exe, for example, set Target:

"C:\Program Files\Reader\Reader.exe" -today -tommorow

when i run it that way, i get:

System.Environment.GetCommandLineArgs().Length = 3
System.Environment.GetCommandLineArgs(0) = "C:\Program Files\Reader\Reader.exe"
System.Environment.GetCommandLineArgs(1) = "-today"
System.Environment.GetCommandLineArgs(2) = "-tomorrow"

which is what i would expect...

If it helps, I am using windows XP

Allen
  • 927
  • 8
  • 19
  • I'm not sure how Windows handles the its open with multiple files, it may run your program multiple times with one file given to each instance it starts. – Kratz May 15 '12 at 18:03

1 Answers1

1

From what I can find, Windows will not send multiple file names on the same command line when you select multiple files in explorer. It will for some applications, start as separate instance of the program and pass each instance one of the files.

Kratz
  • 4,280
  • 3
  • 32
  • 55
  • that makes sense... i wonder what determines if it just opens the first one (like it has been for me) or if it tries to start multiple instances – Allen Feb 28 '13 at 19:39