■ Parallel 클래스 : For 정적 메소드를 사용해 디렉토리 크기 구하기 예제
------------------------------------------------------------------------------------------------------------------------
using System;
long directorySize = GetDirectorySize("c:\\temp", true);
Console.WriteLine("디렉토리 크기 : {0:n0} 바이트", directorySize);
------------------------------------------------------------------------------------------------------------------------
■ Parallel 클래스 : For 정적 메소드를 사용해 디렉토리 크기 구하기
------------------------------------------------------------------------------------------------------------------------
using System.IO;
using System.Threading;
using System.Threading.Tasks;
#region 디렉토리 크기 구하기 - GetDirectorySize(directoryPath, searchAllDirectory)
/// <summary>
/// 디렉토리 크기 구하기
/// </summary>
/// <param name="directoryPath">디렉토리 경로</param>
/// <param name="searchAllDirectory">모든 디렉토리 검색 여부</param>
/// <returns>디렉토리 크기</returns>
public long GetDirectorySize(string directoryPath, bool searchAllDirectory)
{
long directorySize = 0L;
string[] filePathArray = Directory.GetFiles(directoryPath);
foreach(string filePath in filePathArray)
{
Interlocked.Add(ref directorySize, new FileInfo(filePath).Length);
}
if(searchAllDirectory)
{
string[] childDirectoryPathArray = Directory.GetDirectories(directoryPath);
Parallel.For<long>
(
0,
childDirectoryPathArray.Length,
() => 0, (i, loop, childDirectorySize) =>
{
if((File.GetAttributes(childDirectoryPathArray[i]) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
{
childDirectorySize += GetDirectorySize(childDirectoryPathArray[i], true);
return childDirectorySize;
}
return 0;
},
(x) => Interlocked.Add(ref directorySize, x)
);
}
return directorySize;
}
#endregion
------------------------------------------------------------------------------------------------------------------------
'C# > Common' 카테고리의 다른 글
[C#/COMMON] DirectoryInfo 클래스 : EnumerateDirectories 메소드를 사용해 디렉토리 크기 구하기 (0) | 2018.09.21 |
---|---|
[C#/COMMON] 파일 경로 축약하기 (0) | 2018.09.17 |
[C#/COMMON] 파일 크기 문자열 구하기 (0) | 2018.09.17 |
[C#/COMMON] NTFS MFT를 사용해 디렉토리 크기 구하기 (0) | 2018.09.16 |
[C#/COMMON] DirectoryInfo 클래스 : EnumerateDirectories 메소드를 사용해 디렉토리 크기 구하기 (0) | 2018.09.13 |
[C#/COMMON] Parallel 클래스 : For 정적 메소드를 사용해 디렉토리 크기 구하기 (0) | 2018.09.13 |
[C#/COMMON] DirectoryInfo 클래스 : EnumerateFiles 메소드를 사용해 디렉토리 크기 구하기 (0) | 2018.09.13 |
[C#/COMMON] FSO를 사용해 디렉토리 크기 구하기 (0) | 2018.09.13 |
[C#/COMMON] 우리나라 전통 색상 코드 (0) | 2018.09.08 |
[C#/COMMON] Convert 클래스 : ChangeType 메소드를 사용해 타입 변환하기 (0) | 2018.09.03 |
[C#/COMMON] 애플리케이션 구성 파일 선택하기 (0) | 2018.08.22 |
댓글을 달아 주세요