将文件夹路径复制到列表框

时间:2013-12-21 04:28:26

标签: vb.net winforms listbox

我设法将多个文件夹路径拖放到列表框,是否可以使用复制/粘贴执行此操作,例如,您在Windows资源管理器上复制多个文件夹,然后使用contextmenu,快捷键将这些文件夹路径粘贴到列表框中,或按钮..

    Private Sub lstFolder_DragDrop(sender As Object, e As Windows.Forms.DragEventArgs) Handles lstFolder.DragDrop           
        Dim directories As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())          
        For Each folder As String In From folders In directories Where Directory.Exists(folders)
            If Not lstFolder.Items.Contains(folder.ToString()) Then
                lstFolder.Items.Add(folder.ToString())
            End If
        Next

    End Sub

    Private Shared Sub lstFolder_DragEnter(sender As Object, e As Windows.Forms.DragEventArgs) Handles lstFolder.DragEnter          
        If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then          
            e.Effect = DragDropEffects.All
        End If
    End Sub

@ Vignesh Kumar

效果很好,有一个问题,如何从文档文件或地址栏复制文件夹位置,到目前为止这是我的代码。

    Dim directories As String() = CType(Clipboard.GetData(Windows.Forms.DataFormats.FileDrop), String())
    'loop through the string array, check if folder exist then adding each folder to the ListBox
    For Each folder As String In From folders In directories Where Directory.Exists(folders)
        If Not lstFolder.Items.Contains(folder.ToString()) Then
            lstFolder.Items.Add(folder.ToString())
        End If
    Next

1 个答案:

答案 0 :(得分:1)

是。使用Clipboard对象

string[] files = (string[])Clipboard.GetData(System.Windows.Forms.DataFormats.FileDrop);

文件或/和文件夹将位于此字符串数组中。