In C#, Directory.GetDirectories() returns the full paths of subfolders as a string[]. Because it is a real array, you get Length, an index like dirs[0], and methods such as Array.Sort right away.
The array is filled completely before the method returns. For a small or medium folder this is simple and fine. For a huge folder, EnumerateDirectories() streams the results and uses less memory.
This is a static method of System.IO.Directory. Call it as Directory.GetDirectories(...); there is no new Directory(). For an object that remembers one folder, see DirectoryInfo.
Syntax
Pass the parent folder. Add a pattern or a search option when you need them.
C#
string root = Path.Combine(Path.GetTempPath(), "codetofun-demo");
string[] dirs = Directory.GetDirectories(root);
Array.Sort(dirs, StringComparer.Ordinal);
foreach (string dir in dirs) {
Console.WriteLine(Path.GetFileName(dir));
}
The order is not guaranteed. Always sort the array before you print it or compare it. Array.Sort(dirs, StringComparer.Ordinal) gives the same order on every operating system.
Library declarations
C#
public static string[] GetDirectories(string path);
public static string[] GetDirectories(string path, string searchPattern);
public static string[] GetDirectories(string path, string searchPattern, SearchOption searchOption);
Parameters
Parameter
Description
path
The folder to look in. It must exist.
searchPattern
Optional. A name filter such as "20*". Default "*".
searchOption
Optional. TopDirectoryOnly (default) or AllDirectories.
Return Value
Return Value
Description
string[]
An array of full paths. It is an empty array (never null) when there are no matching subfolders.
C# Examples
Example 1 — Get Subfolders and Sort the Array
Create three year folders, read them into an array, sort it, and print names.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-getdirs-sorted");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(Path.Combine(root, "2025"));
Directory.CreateDirectory(Path.Combine(root, "2023"));
Directory.CreateDirectory(Path.Combine(root, "2024"));
string[] dirs = Directory.GetDirectories(root);
Array.Sort(dirs, StringComparer.Ordinal);
Console.WriteLine("Length: " + dirs.Length);
for (int i = 0; i < dirs.Length; i++) {
Console.WriteLine(i + ": " + Path.GetFileName(dirs[i]));
}
Directory.Delete(root, true);
}
}
Output
Length: 3
0: 2023
1: 2024
2: 2025
How It Works
Created out of order. The folders are made as 2025, 2023, 2024, so the creation order would give a messy list.
Real array features.dirs.Length is available at once, with no counting loop. A for loop uses each index.
Sort fixes the order.Array.Sort(dirs, StringComparer.Ordinal) puts the full paths in order. They share the same start, so the years come out as 2023, 2024, 2025.
Print names only.Path.GetFileName(dirs[i]) trims each path to its last part, so no machine-specific path is shown.
Example 2 — Compare Top Level and All Levels
Build a small folder tree and count the results of three different calls.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-getdirs-levels");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(Path.Combine(root, "2024", "jan"));
Directory.CreateDirectory(Path.Combine(root, "2024", "feb"));
Directory.CreateDirectory(Path.Combine(root, "2025"));
Directory.CreateDirectory(Path.Combine(root, "archive"));
string[] top = Directory.GetDirectories(root);
string[] all = Directory.GetDirectories(root, "*", SearchOption.AllDirectories);
string[] years = Directory.GetDirectories(root, "20*");
Console.WriteLine("Top level: " + top.Length);
Console.WriteLine("All levels: " + all.Length);
Console.WriteLine("Names starting with 20: " + years.Length);
Directory.Delete(root, true);
}
}
Output
Top level: 3
All levels: 5
Names starting with 20: 2
How It Works
Five folders in total. The tree has 2024, 2025, and archive at the top, and jan and feb inside 2024.
Top level sees three. The default only reports direct children, so jan and feb are not in the first array.
AllDirectories sees five. The option makes the search go down every branch, adding the two month folders.
Pattern keeps two."20*" matches 2024 and 2025 only, and archive is left out. Counting needs no sort.
Example 3 — Empty Folder vs Missing Folder
An empty folder gives an empty array. A missing folder throws an exception.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-getdirs-empty");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string[] none = Directory.GetDirectories(root);
Console.WriteLine("Subfolders: " + none.Length);
Console.WriteLine("Is null: " + (none == null));
try {
Directory.GetDirectories(Path.Combine(root, "missing"));
} catch (DirectoryNotFoundException) {
Console.WriteLine("Folder not found");
}
Directory.Delete(root);
}
}
Output
Subfolders: 0
Is null: False
Folder not found
How It Works
An empty folder is not an error. The call succeeds and returns an array with zero items, so Length is 0.
Never null. The array exists, so none == null is False. You can loop over it without a null check.
A missing folder is an error. There is nothing to read at missing, so DirectoryNotFoundException is thrown and caught.
Frequently Asked Questions
It returns a string[] of full subfolder paths. The array is empty, never null, when there are no matching subfolders.
Only when you pass SearchOption.AllDirectories. By default it returns the direct subfolders only.
Use Path.GetFileName(path) on each item. It returns the last part, such as "docs", instead of the full path.
Not reliably. Call Array.Sort(dirs, StringComparer.Ordinal) when you need a stable order.
Use GetDirectories when you want an array with Length and indexes, or when the folder is small. Use EnumerateDirectories for very large folders or when you may stop early.