Copy a directory with DirectoryInfo : DirectoryInfo « File Directory « VB.Net






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

1.Create DirectoryInfo class on the specified path.
2.DirectoryInfo.Create Creates a directory.
3.DirectoryInfo.CreateSubdirectory
4.DirectoryInfo.Delete deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.
5.DirectoryInfo.Delete deletes this DirectoryInfo if it is empty.
6.DirectoryInfo.EnumerateDirectories Method (String, SearchOption)
7.DirectoryInfo.EnumerateDirectories returns an enumerable collection of directory information in the current directory.
8.DirectoryInfo.Exists Property gets a value indicating whether the directory exists.
9.DirectoryInfo.GetAccessControl
10.DirectoryInfo.GetDirectories returns an array of directories in the current DirectoryInfo matching the given search criteria.
11.DirectoryInfo.GetDirectories (String, SearchOption)
12.DirectoryInfo.GetDirectories returns the subdirectories of the current directory.
13.DirectoryInfo.GetFiles returns a file list from the current directory matching the given search pattern.
14.DirectoryInfo.GetFiles returns a file list from the current directory.
15.DirectoryInfo.MoveTo moves a DirectoryInfo instance and its contents to a new path.
16.DirectoryInfo.Name Property gets the name of this DirectoryInfo instance.
17.DirectoryInfo.Parent Property gets the parent directory of a specified subdirectory.
18.DirectoryInfo.Root Property gets the root portion of a path.
19.DirectoryInfo Class Exposes instance methods for creating, moving, and enumerating through directories and subdirectories
20.Create and delete directory with DirectoryInfo
21.FileSystemInfo Class provides the base class for both FileInfo and DirectoryInfo objects.