C# Directory GetDirectories() Method

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

Definition

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));
}
CallWhat it does
GetDirectories(path)Returns the direct subfolders of path.
GetDirectories(path, "20*")Only subfolders whose names start with 20.
GetDirectories(path, "*", SearchOption.AllDirectories)Includes subfolders at every level below path.

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

ParameterDescription
pathThe folder to look in. It must exist.
searchPatternOptional. A name filter such as "20*". Default "*".
searchOptionOptional. TopDirectoryOnly (default) or AllDirectories.

Return Value

Return ValueDescription
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);
    }
}

How It Works

  1. Created out of order. The folders are made as 2025, 2023, 2024, so the creation order would give a messy list.
  2. Real array features. dirs.Length is available at once, with no counting loop. A for loop uses each index.
  3. 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.
  4. 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);
    }
}

How It Works

  1. Five folders in total. The tree has 2024, 2025, and archive at the top, and jan and feb inside 2024.
  2. Top level sees three. The default only reports direct children, so jan and feb are not in the first array.
  3. AllDirectories sees five. The option makes the search go down every branch, adding the two month folders.
  4. 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);
    }
}

How It Works

  1. An empty folder is not an error. The call succeeds and returns an array with zero items, so Length is 0.
  2. Never null. The array exists, so none == null is False. You can loop over it without a null check.
  3. 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.

Next: GetDirectoryRoot()

Get the root of a path, such as C:\ or /, without touching the disk.

GetDirectoryRoot() 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.

14 people found this page helpful