---
title: "How do you calculate the total size of all files in a directory (including subdirectories)?"  
description: "How do you calculate the total size of all files in a directory (including subdirectories)?"  
author: "Ravi Vishwakarma"  
published: 2025-05-13  
updated: 2025-05-13  
canonical: https://www.mindstick.com/interview/34111/how-do-you-calculate-the-total-size-of-all-files-in-a-directory-including-subdirectories  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you calculate the total size of all files in a directory (including subdirectories)?

To calculate the **total size of all files in a directory**, including **all subdirectories**, you can use `DirectoryInfo` and recursively enumerate files using `EnumerateFiles()` with the `SearchOption.AllDirectories` option.

### Example: Calculate total size (in bytes)

```cs
using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        string path = @"C:\MyFolder";
        long totalSize = GetDirectorySize(path);

        Console.WriteLine($"Total size: {totalSize} bytes");
        Console.WriteLine($"Total size: {totalSize / 1024.0 / 1024.0:F2} MB");
    }

    static long GetDirectorySize(string folderPath)
    {
        if (!Directory.Exists(folderPath))
            return 0;

        return Directory.EnumerateFiles(folderPath, "*", SearchOption.AllDirectories)
                        .Select(f => new FileInfo(f))
                        .Sum(fi => fi.Length);
    }
}
```

### Notes:

1. `Directory.EnumerateFiles(...)` is memory-efficient compared to `GetFiles(...)` because it streams results.
2. This method handles **deep nested folders** and **hidden files**.
3. If you want to **ignore certain file types**, you can filter them with `.Where(...)`.

### Optional: Handle exceptions (e.g., permission errors)

Wrap file access in try-catch if you expect restricted folders:

```cs
long safeSize = 0;
foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories))
{
    try
    {
        safeSize += new FileInfo(file).Length;
    }
    catch { /* Ignore inaccessible files */ }
}
```

## Answers

### Answer by Ravi Vishwakarma

To calculate the **total size of all files in a directory**, including **all subdirectories**, you can use `DirectoryInfo` and recursively enumerate files using `EnumerateFiles()` with the `SearchOption.AllDirectories` option.

### Example: Calculate total size (in bytes)

```cs
using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        string path = @"C:\MyFolder";
        long totalSize = GetDirectorySize(path);

        Console.WriteLine($"Total size: {totalSize} bytes");
        Console.WriteLine($"Total size: {totalSize / 1024.0 / 1024.0:F2} MB");
    }

    static long GetDirectorySize(string folderPath)
    {
        if (!Directory.Exists(folderPath))
            return 0;

        return Directory.EnumerateFiles(folderPath, "*", SearchOption.AllDirectories)
                        .Select(f => new FileInfo(f))
                        .Sum(fi => fi.Length);
    }
}
```

### Notes:

1. `Directory.EnumerateFiles(...)` is memory-efficient compared to `GetFiles(...)` because it streams results.
2. This method handles **deep nested folders** and **hidden files**.
3. If you want to **ignore certain file types**, you can filter them with `.Where(...)`.

### Optional: Handle exceptions (e.g., permission errors)

Wrap file access in try-catch if you expect restricted folders:

```cs
long safeSize = 0;
foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories))
{
    try
    {
        safeSize += new FileInfo(file).Length;
    }
    catch { /* Ignore inaccessible files */ }
}
```


---

Original Source: https://www.mindstick.com/interview/34111/how-do-you-calculate-the-total-size-of-all-files-in-a-directory-including-subdirectories

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
