Copy a directory with DirectoryInfo
Imports System
Imports System.IO
Class CopyDir
Shared Sub CopyAll(ByVal source As DirectoryInfo, ByVal target As DirectoryInfo)
If (source.FullName.ToLower() = target.FullName.ToLower()) Then
Return
End If
If Directory.Exists(target.FullName) = False Then
Directory.CreateDirectory(target.FullName)
End If
For Each fi As FileInfo In source.GetFiles()
Console.WriteLine("Copying {0}\{1}", target.FullName, fi.Name)
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), True)
Next
For Each diSourceSubDir As DirectoryInfo In source.GetDirectories()
Dim nextTargetSubDir As DirectoryInfo = target.CreateSubdirectory(diSourceSubDir.Name)
CopyAll(diSourceSubDir, nextTargetSubDir)
Next
End Sub
Shared Sub Main()
Dim sourceDirectory As String = "c:\\s"
Dim targetDirectory As String = "c:\\t"
Dim diSource As DirectoryInfo = New DirectoryInfo(sourceDirectory)
Dim diTarget As DirectoryInfo = New DirectoryInfo(targetDirectory)
CopyAll(diSource, diTarget)
End Sub
End Class
Related examples in the same category