Calculate Directory Size : Directory Size « File Directory Stream « C# / CSharp Tutorial






using System;
using System.IO;

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

    }

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

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

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

        return totalSize;
    }
}








15.17.Directory Size
15.17.1.Calculate Directory Size
15.17.2.Display Dir information recursivly