C# 디렉터리 하위의 모든 파일 경로 구하기

Benedictus Park·2022년 12월 15일
0

C#

목록 보기
2/2
post-thumbnail
string[] allPathes = new string[0];

void ArrayAppender(ref string[] a, string[] b){
    string[] result = new string[a.Length + b.Length];
    for(int i = 0; i < a.Length; i++){
        result[i] = a[i];
    }
    for(int i = a.Length; i < a.Length + b.Length; i++){
        result[i] = b[i - a.Length];
    }
    
    a = result;
}

private void GetAllFilePathes(string dirPath){       
    DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
    FileInfo[] files = dirInfo.GetFiles();
    DirectoryInfo[] dirs = dirInfo.GetDirectories();
    string[] pathes = new string[files.Length];

    for (int i = 0; i < files.Length; i++){
        pathes[i] = files[i].FullName;
    }

    ArrayAppender.Append(ref allFilePathes, pathes);

    for(int i = 0; i < dirs.Length; i++){
        GetAllFilePathes(dirs[i]);
    }
}
  • 재귀 함수를 이용하면 손쉽게 특정 디렉터리 하위의 모든 파일의 경로를 구할 수 있다.

0개의 댓글