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;
}
Published Sunday, September 28, 2003 11:47 AM by puzzlehacker

Comments

# re: Get Directory Size in .Net

Monday, December 22, 2003 9:51 AM by jalbitz

# re: Get Directory Size in .Net

thanks.

Monday, July 24, 2006 6:18 PM by asdf

# re: Get Directory Size in .Net

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;

Wednesday, July 28, 2010 10:47 AM by Seth007

Leave a Comment

(required) 
(required) 
(optional)
(required)