Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to be able to drag and drop a folder to a
ListView.

When i drop the folder to the listview it shall only diplay the files ind the folder,
in the list view - and not only the folder.

So Droping a folder with 4 .txt files - should only display the .txt files in the filelist.

Code i provided works for files, but i also want it to works with Folder drop.

Can anyone help here? :-)
Thanx.

Columns in the ListView :
FileList.View = System.Windows.Forms.View.Details
FileList.Columns.Add("FileName", 100, HorizontalAlignment.Left)


What I have tried:

Private Sub FileList_DragDrop(sender As Object, e As DragEventArgs) Handles FileList.DragDrop
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then

         Dim MyFiles() As String
         Dim i As Integer
         Dim FileItem As New ListViewItem

         MyFiles = e.Data.GetData(DataFormats.FileDrop)


         For i = 0 To MyFiles.Length - 1

           FilePath = Path.GetDirectoryName(MyFiles(i))
           FileItem.Text = Path.GetFileNameWithoutExtension(MyFiles(i))

           FileList.Items.Add(FileItem.Text)

         Next
     End If
End Sub



Private Sub FileList_DragEnter(sender As Object, e As DragEventArgs) Handles FileList.DragEnter
     If e.Data.GetDataPresent(DataFormats.FileDrop) Then
         e.Effect = DragDropEffects.All
     End If
End Sub
Posted
Updated 13-Oct-20 22:42pm
v2

Believe you are adding a text to list instead of a listviewitem:
VB
FileList.Items.Add(FileItem.Text)

Change this to this and try:
VB
FileList.Items.Add(FileItem)

In case needed, a reference for you: Listview drag and drop from windows explorer in VB.Net[^]
Folder as subitem:
VB
' Loop through the array and add the files to the list.
For i = 0 To MyFiles.Length - 1

    Dim fileDetail As IO.FileInfo
    fileDetail = My.Computer.FileSystem.GetFileInfo(MyFiles(i))

    Dim objShell As Object
    Dim objFolder As Object
    Dim strFileTitle As Object

    objShell = CreateObject("Shell.Application")
    objFolder = objShell.Namespace(fileDetail.DirectoryName)
    strFileTitle = objFolder.GetDetailsOf(objFolder.ParseName(fileDetail.Name), 21)

    Dim objListViewItem As New ListViewItem
    objListViewItem.Text = fileDetail.Name
    objListViewItem.SubItems.Add(strFileTitle)
    objListViewItem.SubItems.Add(fileDetail.Extension)
    objListViewItem.SubItems.Add(fileDetail.FullName)
    ListView1.Items.Add(objListViewItem)

Next
 
Share this answer
 
Comments
Maciej Los 14-Oct-20 4:46am    
5ed!
It took some time but i solved it my self :-)

Add a listview to your form and add the code :

Imports System.IO
Imports System.Net

Public Class Form1
Dim FileExt, FileName, FilePath As String
Dim i As Integer

'Form Load
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            FileList.View = System.Windows.Forms.View.Details

  FileList.Columns.Add("FileName", 350, HorizontalAlignment.Left)
  FileList.Columns.Add("EXT", 60, HorizontalAlignment.Center)
  FileList.Columns.Add("FilePath", 350, HorizontalAlignment.Left)
End Sub

'FileListview - Drag And Drop Enter
Private Sub FileList_DragEnter(sender As Object, e As DragEventArgs) Handles     
            FileList.DragEnter

   If e.Data.GetDataPresent(DataFormats.FileDrop) Then
     e.Effect = DragDropEffects.All
   End If
End Sub

    
'FileListView Drag And Drop Folder
Private Sub FileList_DragDrop(sender As Object, e As DragEventArgs) Handles    
            FileList.DragDrop

  If e.Data.GetDataPresent(DataFormats.FileDrop) Then
  Dim MyFiles() As String

   ' Assign the files to an array.
   MyFiles = e.Data.GetData(DataFormats.FileDrop)
   ' Loop through the array and add the files to the list.       

   'FOLDER DROP
    For i = 0 To MyFiles.Length - 1
    'For Each foundFile As String In My.Computer.FileSystem.GetFiles(MyFiles(i), 
     FileIO.SearchOption.SearchTopLevelOnly, "*.*") 'WITHOUT SUBFOLDERS
     
     For Each foundFile As String In My.Computer.FileSystem.GetFiles(MyFiles(i), 
     FileIO.SearchOption.SearchAllSubDirectories, "*.*") 'WITH SUBFOLDERS
                    
      FilePath = Path.GetDirectoryName(foundFile)
      FileName = Path.GetFileNameWithoutExtension(foundFile)
      FileExt = My.Computer.FileSystem.GetFileInfo(foundFile).Extension

      FileList.Items.Add(FileName)

       For Each File As ListViewItem In FileList.Items

         File.SubItems.Add(FileExt)
         File.SubItems.Add(FilePath)
       Next
      Next
     Next
   End If

 FileList.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
End Sub
End Class


How to: Get the Collection of Files in a Directory - Visual Basic | Microsoft Docs[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900