'디렉토리 파일 갯수'에 해당되는 글 1건

  1. 2015.08.26 디렉토리 파일 갯수
C# 에서 단순 디렉토리 갯수를 알고 싶다. 아래 소스 보면 확인이 가능하다. 
(참고로 하위 디렉토리까지 다 확인을 해야 한다)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int GetDirecotoryCount(string filePath)
{
    int count = 0;
    try
    {
        if (System.IO.Directory.Exists(filePath))
        {
            System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(filePath);
            count = directoryInfo.GetFiles("*.*", System.IO.SearchOption.AllDirectories).Length;
        }
    }
    catch
    { ; }
    return count;
}
 

중요한것이 있다.
하위 디렉토리까지 검사 유무 체크하는 것이 있다 GetFiles 에서 옵션 유/무 에 따라서 동작을 한다. 

1. 하위 디렉토리 포함 갯수 확인
 - GetFiles("*.*", System.IO.SearchOption.AllDirectories)
2. 현재 디렉토리 포함하지 않은 갯수 확인
 - GetFiles() 이런식으로 사용하면 쉽게 사용 가능하다.


'Program > C#' 카테고리의 다른 글

C# 파일 및 디렉토리 삭제(휴지통으로...)  (1) 2015.08.26
String.Format 에서 중괄호( { 또는 }) 사용하기  (0) 2015.01.13
강제로 Excetpion 만들기  (0) 2014.12.29
C# 예약어  (0) 2014.10.16
Posted by PARK37
,