Menus

Tuesday, December 4, 2012

Calculate the Size of a Folder/Directory using .NET 4.0

.NET 4.0 introduces 7 New methods to Enumerate Directory and Files in .NET 4.0. All these methods return Enumerable Collections (IEnumerable<T>), which perform better than arrays. We will be using the DirectoryInfo.EnumerateDirectories and DirectoryInfo.EnumerateFiles in this sample which returns an enumerable collection of Directory and File information respectively.

Here’s how to calculate the size of a folder or directory using .NET 4.0 and LINQ. The code also calculates the size of all sub-directories.

C#

using System;
using System.Linq;
using System.IO;

namespace ConsoleApplication3
{
  class Program
  {
    static void Main(string[] args)
    {
      DirectoryInfo dInfo = new DirectoryInfo(@"C:/Articles");
      // set bool parameter to false if you
      // do not want to include subdirectories.
      long sizeOfDir = DirectorySize(dInfo, true);

      Console.WriteLine("Directory size in Bytes : " +
      "{0:N0} Bytes", sizeOfDir);
      Console.WriteLine("Directory size in KB : " +
      "{0:N2} KB", ((double)sizeOfDir) / 1024);
      Console.WriteLine("Directory size in MB : " + 
      "{0:N2} MB", ((double)sizeOfDir) / (1024 * 1024));

      Console.ReadLine();
    }

    static long DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
    {
       // Enumerate all the files
       long totalSize = dInfo.EnumerateFiles()
                    .Sum(file => file.Length);

       // If Subdirectories are to be included
       if (includeSubDir)
       {
          // Enumerate all sub-directories
          totalSize += dInfo.EnumerateDirectories()
                   .Sum(dir => DirectorySize(dir, true));
       }
       return totalSize;
    }
  }
}

VB.NET 10.0 (converted using online tool)

Imports System
Imports System.Linq
Imports System.IO

Module Module1

Sub Main()
   Dim dInfo As New DirectoryInfo("C:/Articles")
   ' set bool parameter to false if you
   ' do not want to include subdirectories.
   Dim sizeOfDir As Long = DirectorySize(dInfo, True)

   Console.WriteLine("Directory size in Bytes : " & _
    "{0:N0} Bytes", sizeOfDir)
   Console.WriteLine("Directory size in KB : " & _
    "{0:N2} KB", (CDbl(sizeOfDir)) / 1024)
   Console.WriteLine("Directory size in MB : " & _
    "{0:N2} MB", (CDbl(sizeOfDir)) / (1024 * 1024))

   Console.ReadLine()
End Sub

Private Function DirectorySize(ByVal dInfo As DirectoryInfo, _
   ByVal includeSubDir As Boolean) As Long
   ' Enumerate all the files
   Dim totalSize As Long = dInfo.EnumerateFiles() _
     .Sum(Function(file) file.Length)

   ' If Subdirectories are to be included
   If includeSubDir Then
     ' Enumerate all sub-directories
     totalSize += dInfo.EnumerateDirectories() _
      .Sum(Function(dir) DirectorySize(dir, True))
   End If
   Return totalSize
End Function

End Module


OUTPUT

I got the following output after running this code.

image

To confirm that the code worked as expected, I opened Windows Explorer > C Drive > Right clicked the folder > Properties. Here is the screenshot that matches the output we got from our code

image

No comments:

Post a Comment