Get Directory Size in .Net

Here is a code snippet that someone else might find helpful. Also if anyone else knows a better way to do this please let me know.
/// Returns the size of the directory in bytes
public static long Size(System.IO.DirectoryInfo dirInfo)
{
  long total = 0;

  foreach(System.IO.FileInfo file in dirInfo.GetFiles())
    total += file.Length;
  
  foreach(System.IO.DirectoryInfo dir in dirInfo.GetDirectories())
    total += Size(dir);

  return total;
}

1 Comment

  • public long getDirectorySize ( System . IO . DirectoryInfo dirInfo )
    {
    long total = 0;

    foreach ( System.IO.FileInfo file in dirInfo . GetFiles ("*",SearchOption.AllDirectories )
    total += file . Length;

    return total;

Comments have been disabled for this content.