첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ 디렉토리 복사하기 예제

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

 

728x90

 

▶ 디렉토리 복사하기

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
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요