.NET
Directory Size
C# Programming
File Management
Coding Tips

What's the best way to calculate the size of a directory in .NET?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The best general-purpose way to calculate a directory size in .NET is to enumerate files recursively and sum their lengths lazily. The important details are not the addition itself. They are how you avoid loading everything into memory, how you handle access errors, and whether you want to follow subdirectories and reparse points.

Prefer Lazy Enumeration

A good baseline is Directory.EnumerateFiles and Directory.EnumerateDirectories rather than GetFiles and GetDirectories.

Why? Because Enumerate... yields items lazily instead of building the entire list up front. That matters for large directory trees.

A Simple Recursive Implementation

csharp
1using System;
2using System.IO;
3
4public static class DirectorySizer
5{
6    public static long GetDirectorySize(string path)
7    {
8        long total = 0;
9
10        foreach (string file in Directory.EnumerateFiles(path))
11        {
12            total += new FileInfo(file).Length;
13        }
14
15        foreach (string dir in Directory.EnumerateDirectories(path))
16        {
17            total += GetDirectorySize(dir);
18        }
19
20        return total;
21    }
22}
23
24Console.WriteLine(DirectorySizer.GetDirectorySize(@"C:	emp"));

This is easy to understand and works for many normal cases.

Add Error Handling for Real Systems

In production code, some directories may be inaccessible, deleted during traversal, or protected by permissions. A robust version should handle those cases.

csharp
1using System;
2using System.IO;
3
4public static class DirectorySizer
5{
6    public static long GetDirectorySizeSafe(string path)
7    {
8        long total = 0;
9
10        try
11        {
12            foreach (string file in Directory.EnumerateFiles(path))
13            {
14                try
15                {
16                    total += new FileInfo(file).Length;
17                }
18                catch (IOException) { }
19                catch (UnauthorizedAccessException) { }
20            }
21
22            foreach (string dir in Directory.EnumerateDirectories(path))
23            {
24                total += GetDirectorySizeSafe(dir);
25            }
26        }
27        catch (IOException) { }
28        catch (UnauthorizedAccessException) { }
29
30        return total;
31    }
32}

Whether you skip errors or fail fast depends on the tool you are building.

If the directory tree contains junctions or symbolic links, naive recursion can double-count data or even loop into unexpected parts of the filesystem.

If that matters, inspect directory attributes and skip reparse points.

csharp
1var info = new DirectoryInfo(path);
2if ((info.Attributes & FileAttributes.ReparsePoint) != 0)
3{
4    return 0;
5}

This is especially important on Windows systems with junction-heavy layouts.

Parallelism Is Not Automatically Better

You can parallelize directory traversal, but it is not always the best choice. Disk I/O is often the bottleneck, and aggressive parallelism can increase contention rather than improve speed.

For most applications, the better first optimization is lazy enumeration plus careful error handling. Only add parallelism if profiling shows a real benefit on your workload and storage type.

Synchronous Versus UI-Friendly Execution

Directory size calculation can take noticeable time on large trees. In desktop apps, run it off the UI thread so the interface does not freeze.

That is more important than forcing the filesystem API itself to be asynchronous.

What "Best" Usually Means

If you want:

  • simplest code: basic recursion
  • better scalability: lazy enumeration with robust error handling
  • full correctness in complex environments: skip or explicitly handle reparse points and permission failures

The right answer depends on whether you are building a quick utility or a hardened disk-usage tool.

Common Pitfalls

A common mistake is using GetFiles on a huge tree, which allocates more than necessary.

Another mistake is assuming every file and directory is accessible. Real filesystem traversal often encounters permission or race-condition errors.

Developers also often forget about junctions and symlinks, which can distort totals or create recursion surprises.

Summary

  • Use lazy enumeration to avoid loading the whole directory tree into memory.
  • Sum file lengths recursively for a clean baseline implementation.
  • Add error handling for inaccessible or disappearing files.
  • Be careful with reparse points and symbolic links.
  • Run the calculation off the UI thread if the directory may be large.

Course illustration
Course illustration

All Rights Reserved.