Monday, February 28, 2005

C#: directory size

public static long DirSize(DirectoryInfo d)
{
long Size = 0;
// Add file sizes.
FileInfo[] fis = d.GetFiles();
foreach (FileInfo fi in fis)
{
Size += fi.Length;
}
// Add subdirectory sizes.
DirectoryInfo[] dis = d.GetDirectories();
foreach (DirectoryInfo di in dis)
{
Size += DirSize(di);
}
return(Size);
}

//using:
DirectoryInfo dir_info = new DirectoryInfo(path_to_the_directory);
long Size = DirSize(dir_info);

No comments: