Answered How to Copy a folder from Explorer to the Desktop?

jumper77

Member
Joined
Jan 23, 2019
Messages
15
Location
Jackson Tennessee
Programming Experience
Beginner
I'm trying to copy a folder to the desktop. My code isn't working though. It copies all the files within the folder to the desktop. What do I need to do to make it copy just the directory.

VB.NET:
    Private Sub ListView1_DragDrop(sender As Object, e As DragEventArgs) Handles ListView1.DragDrop
        Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
        For Each item In files
            If Directory.Exists(Path.Combine(CStr(item), DesktopPath & "\")) Then
                'copies all the files within directory, but not the folder itself
                My.Computer.FileSystem.CopyDirectory(CStr(item), DesktopPath)
            Else
                File.Copy(CStr(item), Path.Combine(DesktopPath, Path.GetFileName(CStr(item))))
            End If
        Next
    End Sub
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,516
Location
Norway
Programming Experience
10+
This part does not make sense:
Path.Combine(CStr(item), DesktopPath & "\")
docs said:
If path2 contains an absolute path, this method returns path2.
Therefore the result of that code will always be the desktop path.

CopyDirectory behaviour is also expected:
docs said:
Copies the contents of a directory to another directory.
What you must do is create the destination folder first (if not exist) and copy the files there. You can get the directory name with Path.GetFileName method.
 
Top Bottom