방법: 디렉터리 복사

이 예제에서는 I/O 클래스를 사용하여 디렉터리를 한 위치에서 다른 위치로 복사하는 방법을 보여 줍니다. 이 예제에서 사용자는 하위 디렉터리도 복사할지 여부를 지정할 수 있습니다. 하위 디렉터리도 복사하는 경우 이 예제의 메서드는 복사할 디렉터리가 더 이상 없을 때까지 각 하위 디렉터리에서 자신을 호출하여 이러한 하위 디렉터리를 재귀적으로 복사합니다.

예제

Imports System
Imports System.IO

Class DirectoryCopyExample

    Shared Sub Main()
        DirectoryCopy(".", ".\\temp", True)
    End Sub 'Main

    Private Shared Sub DirectoryCopy( _
        ByVal sourceDirName As String, _
        ByVal destDirName As String, _
        ByVal copySubDirs As Boolean)

        Dim dir As DirectoryInfo = New DirectoryInfo(sourceDirName)
        Dim dirs As DirectoryInfo() = dir.GetDirectories()

        ' If the source directory does not exist, throw an exception.
        If Not dir.Exists Then
            Throw New DirectoryNotFoundException( _
                "Source directory does not exist or could not be found: " _
                + sourceDirName)
        End If

        ' If the destination directory does not exist, create it.
        If Not Directory.Exists(destDirName) Then
            Directory.CreateDirectory(destDirName)
        End If

        ' Get the file contents of the directory to copy.
        Dim files As FileInfo() = dir.GetFiles()

        For Each file In files

            ' Create the path to the new copy of the file.
            Dim temppath As String = Path.Combine(destDirName, file.Name)

            ' Copy the file.
            file.CopyTo(temppath, False)
        Next file

        ' If copySubDirs is true, copy the subdirectories.
        If copySubDirs Then

            For Each subdir In dirs

                ' Create the subdirectory.
                Dim temppath As String = _
                    Path.Combine(destDirName, subdir.Name)

                ' Copy the subdirectories.
                DirectoryCopy(subdir.FullName, temppath, copySubDirs)
            Next subdir
        End If
    End Sub
End Class
using System;
using System.IO;

class DirectoryCopyExample
{
    static void Main()
    {
        DirectoryCopy(".", @".\temp", true);
    }

    private static void DirectoryCopy(
        string sourceDirName, string destDirName, bool copySubDirs)
    {
      DirectoryInfo dir = new DirectoryInfo(sourceDirName);
      DirectoryInfo[] dirs = dir.GetDirectories();

      // If the source directory does not exist, throw an exception.
        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        // If the destination directory does not exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }


        // Get the file contents of the directory to copy.
        FileInfo[] files = dir.GetFiles();

        foreach (FileInfo file in files)
        {
            // Create the path to the new copy of the file.
            string temppath = Path.Combine(destDirName, file.Name);

            // Copy the file.
            file.CopyTo(temppath, false);
        }

        // If copySubDirs is true, copy the subdirectories.
        if (copySubDirs)
        {

            foreach (DirectoryInfo subdir in dirs)
            {
                // Create the subdirectory.
                string temppath = Path.Combine(destDirName, subdir.Name);

                // Copy the subdirectories.
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
}

참고 항목

작업

방법: 디렉터리 목록 만들기

개념

기본 파일 I/O

공통적인 I/O 작업