Calculate Directory Size : Directory : File Stream C# Source Code


Custom Search

C# Source Code » File Stream » Directory »

 

Calculate Directory Size








    
 

using System;
using System.IO;

class MainClass {
    static void Main(string[] args) {
        DirectoryInfo dir = new DirectoryInfo("c:\\");
        Console.WriteLine("Total size: " + CalculateDirectorySize(dir, true).ToString() + " bytes.");

    }

    static long CalculateDirectorySize(DirectoryInfo directory, bool includeSubdirectories) {
        long totalSize = 0;

        FileInfo[] files = directory.GetFiles();
        foreach (FileInfo file in files) {
            totalSize += file.Length;
        }

        if (includeSubdirectories) {
            DirectoryInfo[] dirs = directory.GetDirectories();
            foreach (DirectoryInfo dir in dirs) {
                totalSize += CalculateDirectorySize(dir, true);
            }
        }

        return totalSize;
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo File Stream
» Directory