Calculating the overall size of a directory

Import System.IO namespace

using System.IO;

Create new class its name to CalculateDirectorySize and write this below code on CalculateDirectorySize class.

public long CalculateDirectorySize(DirectoryInfo directorypath, 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 = directorypath.GetDirectories();
        foreach (DirectoryInfo dir in dirs)
        {
            totalSize += CalculateDirectorySize(dir, true);
        }
    }
    return totalSize;
}

Add a Button and change its name to GetCalculate and write this below code on Click event handler.

private void GetCalculate_Click(object sender, EventArgs e)
{
    MessageBox.Show(CalculateDirectorySize(new DirectoryInfo( @"C:\WINDOWS"), true).ToString());
}