Computing the size of a folder

  • .NET

The .Net framework does not provide any function to compute the size of all the files in a folder (and its subfolders). As it is not very hard to do here's the code:

public static long FolderSize(string path)
{
    long size = 0;
    DirectoryInfo directoryInfo = new DirectoryInfo(path);
    IEnumerable<FileInfo> files = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
    foreach (FileInfo file in files)
    {
        size += file.Length;
    }

    return size;
}
Follow me:
Enjoy this blog?Buy Me A CoffeeDonate with PayPal

Leave a reply