简体   繁体   中英

How to open multiple files using “open with” VB.net? (command line arguments)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM