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
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.
Whether you skip errors or fail fast depends on the tool you are building.
Watch Out for Reparse Points and Symlinks
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.
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.

