C# Directory EnumerateFileSystemEntries() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, Directory.EnumerateFileSystemEntries() returns the full paths of both files and folders inside a folder. It is the lazy version, so a foreach receives one path at a time while the disk is read.

Use it for everything in one list. For only files use EnumerateFiles(), and for only folders use EnumerateDirectories().

This is a static method of System.IO.Directory. Call it as Directory.EnumerateFileSystemEntries(...); there is no new Directory(). For an object that remembers one folder, see DirectoryInfo.

Syntax

The entries are just strings, so use Directory.Exists if you need to tell a folder from a file.

C#
string root = Path.Combine(Path.GetTempPath(), "codetofun-demo");

foreach (string entry in Directory.EnumerateFileSystemEntries(root)) {
    Console.WriteLine(Path.GetFileName(entry));
}
CallWhat it does
EnumerateFileSystemEntries(path)Streams every file and folder directly inside path.
EnumerateFileSystemEntries(path, "*.txt")Only entries whose name matches. It can match a folder named like a text file too.
EnumerateFileSystemEntries(path, "*", SearchOption.AllDirectories)Includes every level below path.

The starting folder itself is not in the sequence. Sort the names before printing, because the file system picks the order.

Library declarations
C#
public static IEnumerable<string> EnumerateFileSystemEntries(string path);
public static IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern);
public static IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern, SearchOption searchOption);

Parameters

ParameterDescription
pathThe folder to look in. It must exist.
searchPatternOptional. A name filter. The default is "*".
searchOptionOptional. TopDirectoryOnly (default) or AllDirectories.

Return Value

Return ValueDescription
IEnumerable<string>A lazy sequence of full paths for files and folders. It is empty for an empty folder.

C# Examples

Example 1 — Label Each Entry as File or Folder

List a small folder and mark every entry, with the labels sorted.

C#
using System;
using System.Collections.Generic;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-enumentry-mixed");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(Path.Combine(root, "sub"));
        File.WriteAllText(Path.Combine(root, "a.txt"), "1");
        File.WriteAllText(Path.Combine(root, "b.txt"), "2");

        List<string> lines = new List<string>();
        foreach (string entry in Directory.EnumerateFileSystemEntries(root)) {
            string kind = Directory.Exists(entry) ? "[dir] " : "[file] ";
            lines.Add(kind + Path.GetFileName(entry));
        }
        lines.Sort(StringComparer.Ordinal);

        foreach (string line in lines) {
            Console.WriteLine(line);
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. One list, two kinds. The folder holds one subfolder and two files, and all three come from a single call.
  2. Strings carry no type. The sequence is only text, so Directory.Exists(entry) decides whether to add the [dir] or [file] label.
  3. Sort puts folders first here. Ordinal sorting places [dir] before [file]. That order comes from the label, not from the disk.

Example 2 — Filter Files and Folders by Pattern

Count what matches * and *.txt in the same folder.

C#
using System;
using System.IO;
using System.Linq;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-enumentry-pattern");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(Path.Combine(root, "archive"));
        File.WriteAllText(Path.Combine(root, "notes.txt"), "1");
        File.WriteAllText(Path.Combine(root, "todo.txt"), "2");
        File.WriteAllText(Path.Combine(root, "data.csv"), "3");

        int everything = Directory.EnumerateFileSystemEntries(root).Count();
        int texts = Directory.EnumerateFileSystemEntries(root, "*.txt").Count();
        int starA = Directory.EnumerateFileSystemEntries(root, "a*").Count();

        Console.WriteLine("All entries: " + everything);
        Console.WriteLine("*.txt: " + texts);
        Console.WriteLine("a*: " + starA);

        Directory.Delete(root, true);
    }
}

How It Works

  1. Four entries in total. Three files and the folder archive make four.
  2. Extension filter. "*.txt" keeps notes.txt and todo.txt, but not data.csv or the folder.
  3. A prefix filter. "a*" matches only the folder archive, so patterns apply to folders too.

Example 3 — Count Entries at Every Level

Walk a small tree with AllDirectories and separate folders from files.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-enumentry-deep");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(Path.Combine(root, "sub", "deep"));
        File.WriteAllText(Path.Combine(root, "a.txt"), "1");
        File.WriteAllText(Path.Combine(root, "sub", "b.txt"), "2");
        File.WriteAllText(Path.Combine(root, "sub", "deep", "c.txt"), "3");

        int top = 0;
        foreach (string e in Directory.EnumerateFileSystemEntries(root)) top++;

        int total = 0, folders = 0, files = 0;
        foreach (string e in Directory.EnumerateFileSystemEntries(root, "*", SearchOption.AllDirectories)) {
            total++;
            if (Directory.Exists(e)) folders++; else files++;
        }

        Console.WriteLine("Top level: " + top);
        Console.WriteLine("All levels: " + total);
        Console.WriteLine("Folders: " + folders + ", files: " + files);

        Directory.Delete(root, true);
    }
}

How It Works

  1. The tree. root holds a.txt and sub. sub holds b.txt and deep, which holds c.txt.
  2. Top level is 2. Only direct children count: a.txt and sub. The deeper entries stay hidden without the search option.
  3. All levels is 5. Adding b.txt, deep, and c.txt makes five. Directory.Exists is true for sub and deep, so two folders and three files.

Frequently Asked Questions

The full paths of files and folders together as a lazy IEnumerable<string>, without the starting folder itself.
The items are plain strings. Call Directory.Exists(entry) for folders or File.Exists(entry) for files.
Use it when you need files and folders in one pass, such as for a disk usage report. Use EnumerateFiles when you only care about files.
GetFileSystemEntries reads everything first and returns a string[]. EnumerateFileSystemEntries streams one path at a time.
Yes. The pattern is tested against both files and folders, so "a*" can match a folder called archive.

Next: Exists()

Check whether a folder exists; returns false for files, bad paths, and no permission.

Exists() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful