Home C# Directory Methods GetFileSystemEntries() C# Directory GetFileSystemEntries() Method Definition In C#, Directory.GetFileSystemEntries() returns the full paths of files and folders inside a folder as one string[]. It is the all-in-one version of GetFiles() and GetDirectories().
The array is complete when the call returns. For very large folders, the lazy EnumerateFileSystemEntries() is the better choice.
This is a static method of System.IO.Directory. Call it as Directory.GetFileSystemEntries(...); there is no new Directory(). For an object that remembers one folder, see DirectoryInfo.
Syntax The entries are plain strings. Use Directory.Exists or File.Exists when you must know which is which.
string root = Path.Combine(Path.GetTempPath(), "codetofun-demo");
string[] entries = Directory.GetFileSystemEntries(root);
Array.Sort(entries, StringComparer.Ordinal);
foreach (string entry in entries) {
Console.WriteLine(Path.GetFileName(entry));
} Call What it does GetFileSystemEntries(path)Files and folders directly inside path. GetFileSystemEntries(path, "r*")Only entries whose name starts with r. GetFileSystemEntries(path, "*", SearchOption.AllDirectories)Also includes the contents of every subfolder.
The starting folder itself is not in the array. The order is not guaranteed, so sort before printing. Use Path.GetFileName to show only the names.
Library declarations public static string[] GetFileSystemEntries(string path);
public static string[] GetFileSystemEntries(string path, string searchPattern);
public static string[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption); Parameters Parameter Description pathThe folder to look in. It must exist. searchPatternOptional. A name filter. Default "*". searchOptionOptional. TopDirectoryOnly (default) or AllDirectories.
Return Value Return Value Description string[]An array of full paths for files and folders. Empty, never null, for an empty folder.
C# Examples 1. Everything in one array 2. Pattern with r* 3. Split files from folders Example 1 — List Everything in a Folder Make two files and two folders, read them in one call, and print the sorted names.
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-getentries-all");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(Path.Combine(root, "images"));
Directory.CreateDirectory(Path.Combine(root, "temp"));
File.WriteAllText(Path.Combine(root, "report.docx"), "r");
File.WriteAllText(Path.Combine(root, "budget.xlsx"), "b");
string[] entries = Directory.GetFileSystemEntries(root);
Array.Sort(entries, StringComparer.Ordinal);
Console.WriteLine("Entries: " + entries.Length);
foreach (string entry in entries) {
Console.WriteLine(Path.GetFileName(entry));
}
Directory.Delete(root, true);
}
} Entries: 4
budget.xlsx
images
report.docx
temp How It Works Two kinds in one result. The array holds the folders images and temp with the two files.Sorting mixes them by name. Ordinal sorting looks at letters only, so files and folders interleave.Safe to print. Only the last part of each path is written, so the output is the same on every computer.Example 2 — Filter Entries with a Name Pattern Use "r*" to pick entries by their first letter, whether they are files or folders.
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-getentries-pattern");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(Path.Combine(root, "reports"));
File.WriteAllText(Path.Combine(root, "report.docx"), "1");
File.WriteAllText(Path.Combine(root, "readme.md"), "2");
File.WriteAllText(Path.Combine(root, "notes.txt"), "3");
string[] matches = Directory.GetFileSystemEntries(root, "r*");
Array.Sort(matches, StringComparer.Ordinal);
foreach (string m in matches) {
Console.WriteLine(Path.GetFileName(m));
}
Console.WriteLine("Matches: " + matches.Length);
Directory.Delete(root, true);
}
} readme.md
report.docx
reports
Matches: 3 How It Works A folder named like a file. reports is a folder, and report.docx and readme.md are files. All three start with the letter r.notes.txt is out. It starts with n, so the pattern "r*" does not match it.Character by character. readme.md comes first because a sorts before p. report.docx comes before reports because the dot sorts before s.Example 3 — Count Files and Folders Across All Levels Use AllDirectories, then test each path to separate folders from files.
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-getentries-split");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(Path.Combine(root, "docs"));
Directory.CreateDirectory(Path.Combine(root, "img"));
File.WriteAllText(Path.Combine(root, "a.txt"), "1");
File.WriteAllText(Path.Combine(root, "docs", "b.txt"), "2");
File.WriteAllText(Path.Combine(root, "docs", "c.txt"), "3");
string[] top = Directory.GetFileSystemEntries(root);
string[] all = Directory.GetFileSystemEntries(root, "*", SearchOption.AllDirectories);
int folders = 0, files = 0;
foreach (string entry in all) {
if (Directory.Exists(entry)) folders++;
else if (File.Exists(entry)) files++;
}
Console.WriteLine("Top level: " + top.Length);
Console.WriteLine("All levels: " + all.Length);
Console.WriteLine("Folders: " + folders);
Console.WriteLine("Files: " + files);
Directory.Delete(root, true);
}
} Top level: 3
All levels: 5
Folders: 2
Files: 3 How It Works Tree with two levels. The top has a.txt, docs, and img. Inside docs are b.txt and c.txt.Two array sizes. The default call finds 3 entries. With AllDirectories the two files in docs are added, giving 5.Test each path. The entries are strings, so Directory.Exists and File.Exists decide the kind.Totals add up. 2 folders plus 3 files equals the 5 entries, and the numbers do not depend on disk order.Frequently Asked Questions What does Directory.GetFileSystemEntries return? A string[] of full paths for files and folders together, without the starting folder.
How do I know if an entry is a file or a folder? Test the path with Directory.Exists(path) for a folder or File.Exists(path) for a file.
Can I filter the entries by name? Yes. Pass a pattern such as "r*" or "*.txt". It is tested against files and folders.
What is the difference from EnumerateFileSystemEntries? GetFileSystemEntries builds the whole array first. EnumerateFileSystemEntries streams one path at a time.
Why sort the array before printing? The file system does not promise an order. Array.Sort(entries, StringComparer.Ordinal) gives the same result everywhere.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion