JumpyNET
Centurion
- Joined
- Apr 4, 2005
- Messages
- 196
I have a form with one ListBox in it and use the code below. I have allowed drop to the listbox and plan to drop files to it (both folders and files). Files are added at once and folders are searched for more files and subfolders. But when I add a folder containing alot of files like the C-drive then both the program and explorer hang until all files are added.
I could prevent the program from hanging by adding Application.DoEvents() here and there but of course it has no effect on explorer. So how can I prevent explorer from hanging? I would like to drag&drop other folders to my app from explorer even when the previous folder had not yeat been fully processed.
I could prevent the program from hanging by adding Application.DoEvents() here and there but of course it has no effect on explorer. So how can I prevent explorer from hanging? I would like to drag&drop other folders to my app from explorer even when the previous folder had not yeat been fully processed.
Visual Basic:
Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
' Let's allow files to be dropped.
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
' Accept on files and folders.
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
Dim Items() As Object = CType(e.Data.GetData(DataFormats.FileDrop), System.Object)
Dim File As Object
For Each File In Items
' Let's see if we are dealing with a file or a folder.
If System.IO.File.Exists(File) Then
' It's a file, so we add it to the listbox.
AddFile(File.ToString)
ElseIf System.IO.Directory.Exists(File) Then
' It's a folder, so we must search it for files.
FindFiles(File.ToString)
End If
Next
End If
End Sub
Private Sub AddFile(ByVal FilePath As String)
' Simply add the file to the listbox.
ListBox1.Items.Add(FilePath)
End Sub
Private Sub FindFiles(ByVal Directory As String)
' Find files in current folder.
Try
Dim Files As String() = System.IO.Directory.GetFiles(Directory, "*")
Dim File As String
For Each File In Files
AddFile(File)
Next
Catch
End Try
' Find subfolders in current folder and search them for files too.
Try
Dim Dirs As String() = System.IO.Directory.GetDirectories(Directory, "*")
Dim Dir As String
For Each Dir In Dirs
FindFiles(Dir)
Next
Catch
End Try
End Sub