Get Folder Size - CSharp File IO

CSharp examples for File IO:Directory

Description

Get Folder Size

Demo Code


using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System.IO;//from   ww w  . j ava 2s. co  m
using System;

public class Main{
        public static long GetFolderSize(this DirectoryInfo di)
      {
         Ensure.Argument.NotNull(di, "di parameter cannot ba a null.");
         if (di.Exists)
         {
            //return di.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi => fi.Length);
            var files = di.GetFiles("*.*", SearchOption.AllDirectories);
            long result = 0;
            foreach (FileInfo fileInfo in files)
            {
               result += fileInfo.Length;
            }
            return result;
         }
         else
            return -1;
      }
}

Related Tutorials