Copy folder using

Imports System.IO
Public Class Form1
	Private Sub CopyDirectory(ByVal SourcePath As String, ByVal DestPath As String, Optional ByVal Overwrite As Boolean = False)
		Dim SourceDir As DirectoryInfo = New DirectoryInfo(SourcePath)
		Dim DestDir As DirectoryInfo = New DirectoryInfo(DestPath)
		' the source directory must exist, otherwise throw an exception
		If SourceDir.Exists Then
			' if destination SubDir's parent SubDir does not exist throw an exception
			If Not DestDir.Parent.Exists Then
				Throw New DirectoryNotFoundException _
					("Destination directory does not exist: " + DestDir.Parent.FullName)
			End If
			If Not DestDir.Exists Then
			End If
			'copy all the files of the current directory
			Dim ChildFile As FileInfo
			For Each ChildFile In SourceDir.GetFiles()
				If Overwrite Then
					ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), True)
				' if Overwrite = false, copy the file only if it does not exist
				' this is done to avoid an IOException if a file already exists
				' this way the other files can be copied anyway...
					If Not File.Exists(Path.Combine(DestDir.FullName, ChildFile.Name)) Then
						ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), False)
					End If
				End If
			' copy all the sub-directories by recursively calling this same routine
			Dim SubDir As DirectoryInfo
			For Each SubDir In SourceDir.GetDirectories()
				CopyDirectory(SubDir.FullName, Path.Combine(DestDir.FullName, _
						SubDir.Name), Overwrite)
			Throw New DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName)
	End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	CopyDirectory("C:\zipMe", My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Folder1", False)
End Sub

Copy folder using Shell32 :-

	'Click on Project Menu | Add Reference | Browse | Navigate to C:\Windows\System32\ and select shell32.dll
	'use following code
	Imports Shell32 'Dont forget this
	Public Class Form1
		Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
			If Not IO.Directory.Exists(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Folder1") Then
				'Create Destination directory firt
				IO.Directory.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Folder1")
			End If
			Dim SH As New Shell
			Dim SF As Folder = SH.NameSpace("C:\folderToCopy")  'Path of Folder to be copied
			Dim DF As Folder = SH.NameSpace(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Folder1") ' Destination Path
			DF.CopyHere(SF) 'initiate copy process
		End Sub
	End Class

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s