■ DirectoryInfo 클래스 : EnumerateDirectories 메소드를 사용해 디렉토리 크기 구하기 예제

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

using System;

 

long directorySize = GetDirectorySize("d:\\ARCA");

 

Console.WriteLine(directorySize);

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

 

■ DirectoryInfo 클래스 : EnumerateDirectories 메소드를 사용해 디렉토리 크기 구하기

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

using System.Collections.Generic;

using System.IO;

using System.Linq;

 

#region 디렉토리 크기 구하기 - GetDirectorySize(sourceDirectoryPath)

 

/// <summary>

/// 디렉토리 크기 구하기

/// </summary>

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

/// <returns>디렉토리 크기</returns>

public long GetDirectorySize(string sourceDirectoryPath)

{

    IEnumerable<string> filePathEnumerable = Directory.EnumerateFiles(sourceDirectoryPath);

 

    long fileSize = (from filePath in filePathEnumerable let fileInfo = new FileInfo(filePath) select fileInfo.Length).Sum();

 

    IEnumerable<string> subsidaryDirectoryPathEnumerable = Directory.EnumerateDirectories(sourceDirectoryPath);

 

    long subsidaryDirectorySize = (from subsidaryDirectoryPath in subsidaryDirectoryPathEnumerable

        select GetDirectorySize(subsidaryDirectoryPath)).Sum();

 

    return fileSize + subsidaryDirectorySize;

}

 

#endregion

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요