Copying Folders With All Contents

Submitted by:Jhon Brain

Date added:17 October, 2011

Category:VB.NET

Copying Folders With All Contents Including Other Folders.
VB.net 2010 does not have a built in code to copy full folders with their contents but only files(one by one)!!!

But you can do it by this code

Tags: copying folders

Code Snippet:

Module Logic
Public Function CopyDirectory(ByVal Src As String, ByVal
Dest As String)
CopyDirectory = False
If Not Directory.Exists(Src) Then
CopyDirectory = "%%SRCNOTEXITS%%"
Exit Function
End If
If Dest.Substring(Dest.Length - 1, 1) <>
Path.DirectorySeparatorChar Then
Dest += Path.DirectorySeparatorChar
End If
If Not Directory.Exists(Dest) Then
Directory.CreateDirectory(Dest)
Dim Files As String()
Files = Directory.GetFileSystemEntries(Src)
Dim element As String
For Each element In Files
If Directory.Exists(element) Then
CopyDirectory(element, Dest &
Path.GetFileName(element))
Else
File.Copy(element, Dest &
Path.GetFileName(element), True)
End If
Next
Return True
End Function
End Module
 
 

Comments