첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 디렉토리 복사하기 예제

------------------------------------------------------------------------------------------------------------------------

CopyDirectory("d:\\SOURCE", "d:\\TARGET", true);

------------------------------------------------------------------------------------------------------------------------

 

■ 디렉토리 복사하기

------------------------------------------------------------------------------------------------------------------------

using System.IO;

 

#region 디렉토리 복사하기 - CopyDirectory(sourceDirectoryPath, targetDirectoryPath, recursive)

 

/// <summary>

/// 디렉토리 복사하기

/// </summary>

/// <param name="sourceDirectoryPath">소스 디렉토리 경로</param>

/// <param name="targetDirectoryPath">타겟 디렉토리 경로</param>

/// <param name="recursive">재귀 여부</param>

public void CopyDirectory(string sourceDirectoryPath, string targetDirectoryPath, bool recursive)

{

    DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(sourceDirectoryPath);

 

    DirectoryInfo[] sourceChildDicrectoryInfoArray = sourceDirectoryInfo.GetDirectories();

 

    if(!sourceDirectoryInfo.Exists)

    {

        throw new DirectoryNotFoundException

        (

            string.Format

            (

                "소스 디렉토리가 존재하지 않습니다 : {0}",

                sourceDirectoryPath

            )

        );

    }

 

    if(!Directory.Exists(targetDirectoryPath))

    {

        Directory.CreateDirectory(targetDirectoryPath);

    }

 

    FileInfo[] sourceFileInfoArray = sourceDirectoryInfo.GetFiles();

 

    foreach(FileInfo sourceFileInfo in sourceFileInfoArray)

    {

        string targetFilePath = Path.Combine(targetDirectoryPath, sourceFileInfo.Name);

 

        sourceFileInfo.CopyTo(targetFilePath, false);

    }

 

    if(!recursive)

    {

        return;

    }

 

    foreach(DirectoryInfo sourceChildDirectoryInfo in sourceChildDicrectoryInfoArray)

    {

        string childTargetDirectoryPath = Path.Combine(targetDirectoryPath, sourceChildDirectoryInfo.Name);

 

        CopyDirectory(sourceChildDirectoryInfo.FullName, childTargetDirectoryPath, recursive);

    }

}

 

#endregion

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요