Search This Blog

Monday, February 2, 2015

Get Directory Size C#

C# > IO  > FileInfo > Length

Get the size of the file in bytes. 

Example

Get Directory Size C#


public long DirectorySize(DirectoryInfo dir)
{
       long size = 0;
       foreach (FileInfo file in dir.GetFiles())
       {
              size += file.Length;
       }
      foreach (DirectoryInfo directory in dir.GetDirectories())
       {
              size += DirectorySize(directory);
       }
       return size;
 }

DirectoryInfo dir = new DirectoryInfo(@"C:\Temp");
long size = DirectorySize(dir);