Open multiple files

OSVBNET 1,386 Reputation points
2022-07-18T22:45:17.853+00:00

Hello
To enable opening multiple files (either by multi select and pressing Enter, or multi select and drag-drop) I've set AllowDrop = True
Since the app is not mdi multi tabbed, every file needs to open in a new instance:

Private Sub MainForm_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.Copy

Private Sub MainForm_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
Dim DropFiles() As String = TryCast(e.Data.GetData("FileDrop", True), String())
For Each DropFile As String In DropFiles
System.Diagnostics.Process.Start(Application.ExecutablePath + " " + DropFile)
Next

Once I drop a file it falls to an infinite loop with System.ComponentModel.Win32Exception in System.dll
Any idea or suggestion?
Thanks in advance

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,498 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. LesHay 7,106 Reputation points
    2022-07-18T23:41:42.65+00:00

    Hi
    Code below, based on your post works well. I was unable to rationalize the concatenation of the project application start up folder and the full file path from the collection.
    The only change needed was that path, the other slight changes were my own preferences.

    Option Strict On  
    Option Explicit On  
    Public Class Form1  
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
    		AllowDrop = True  
    	End Sub  
    	Private Sub MainForm_DragEnter(sender As Object, e As DragEventArgs) Handles Me.DragEnter  
    		If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.Copy  
    	End Sub  
    	Private Sub MainForm_DragDrop(sender As Object, e As DragEventArgs) Handles Me.DragDrop  
    		Dim DropFiles() As String = TryCast(e.Data.GetData(DataFormats.FileDrop, True), String())  
    		For Each DropFile As String In DropFiles  
    			Process.Start(DropFile)  
    		Next  
    	End Sub  
    End Class  
    

  2. RLWA32 38,856 Reputation points
    2022-07-19T10:39:59.73+00:00

    Give this a try (error checking omitted) -

        Private Sub Form1_DragEnter(sender As Object, e As DragEventArgs) Handles MyBase.DragEnter  
            If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.Copy  
        End Sub  
      
        Private Sub Form1_DragDrop(sender As Object, e As DragEventArgs) Handles MyBase.DragDrop  
            Dim DropFiles() As String = TryCast(e.Data.GetData(DataFormats.FileDrop, True), String())  
            For Each DropFile As String In DropFiles  
                Using proc As New Process()  
                    proc.StartInfo.FileName = Application.ExecutablePath  
                    proc.StartInfo.Arguments = DropFile  
                    proc.Start()  
                End Using  
            Next  
        End Sub  
      
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            Dim args As String() = Environment.GetCommandLineArgs  
            If (args.Length = 2) Then  
                Text = Text + " - " + args(1)  
            End If  
        End Sub  
      
    
    0 comments No comments