C# Directory

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

Definition and Usage

In C#, Directory is a static class in the System.IO namespace. A directory is just a folder. The class gives you ready-made methods to make a folder with CreateDirectory(), check it with Exists(), list its contents with GetFiles(), and remove it with Delete().

Because it is static, you never write new Directory(). You call Directory.Method(path) and pass the folder path each time. Add using System.IO; at the top of your file to use it.

The Directory Methods index lists every method guide in this series.

Practise only in a temp folder.

Methods such as Delete(path, true) remove a whole folder with no undo. Every example here builds a demo folder inside Path.GetTempPath() and deletes only that folder. Do the same while you learn.

C# Syntax

Common first steps:

C#
using System.IO;

string folder = Path.Combine(Path.GetTempPath(), "codetofun-demo");

Directory.CreateDirectory(folder);                  // make it (no error if it exists)
bool there = Directory.Exists(folder);              // true or false
string[] files = Directory.GetFiles(folder);        // files as an array
string[] subs = Directory.GetDirectories(folder);   // subfolders as an array
Directory.Delete(folder);                           // remove it (must be empty)

Directory vs DirectoryInfo vs Path

Three classes in System.IO work with folder paths. Pick the one that matches the job:

ClassWhat it isGood for
DirectoryA static class. You pass the path to each method.Quick one-off jobs: create, check, list, delete
DirectoryInfoAn object that remembers one folder.Many actions on the same folder, plus properties such as Name and Parent
PathA static class that works only with text.Joining, splitting, and reading parts of a path. It never touches the disk

Which Method Should I Use?

Start from what you want to do:

I want to...UseRemember
Make a folderCreateDirectory()Makes missing parents too and is safe to repeat
Remove a folderDelete()Needs true when the folder has contents
Check that a folder is thereExists()Returns false for files and bad paths
List items into an arrayGetFiles(), GetDirectories()Reads everything first; sort before printing
List items from a very large folderEnumerateFiles()Lazy: one path at a time
Rename or move a folderMove()The destination must not exist
Know where the program is workingGetCurrentDirectory()Relative paths start here

C# Examples

Example 1 — Build a Small Folder Tree, List It, and Delete It

A first program that works only inside a temp demo folder: create two subfolders and a file, list them in sorted order, then clean up.

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

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-hub-demo");
        if (Directory.Exists(root)) Directory.Delete(root, true);

        Directory.CreateDirectory(Path.Combine(root, "docs"));
        Directory.CreateDirectory(Path.Combine(root, "images"));
        File.WriteAllText(Path.Combine(root, "readme.txt"), "hello");

        List<string> names = new List<string>();
        foreach (string entry in Directory.EnumerateFileSystemEntries(root)) {
            names.Add(Path.GetFileName(entry));
        }
        names.Sort(StringComparer.Ordinal);
        foreach (string name in names) {
            Console.WriteLine(name);
        }

        Directory.Delete(root, true);
        Console.WriteLine("Exists after delete: " + Directory.Exists(root));
    }
}

How It Works

  1. A private demo path. Path.GetTempPath() plus a fixed name gives a folder that belongs to this lesson. The first if removes a leftover from an earlier run.
  2. Build the tree. CreateDirectory makes the two subfolders, and File.WriteAllText adds readme.txt to the root.
  3. List, then sort. EnumerateFileSystemEntries gives files and folders in any order, so the names go into a List that is sorted with StringComparer.Ordinal.
  4. Clean up. Delete(root, true) removes everything the program made, and Exists confirms it with False.

Example 2 — Directory (Static) vs DirectoryInfo (Object)

Do the same job twice: once with the static Directory class and once with a DirectoryInfo object that remembers the folder.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-hub-compare");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        Console.WriteLine(Directory.Exists(root));
        Console.WriteLine(Directory.GetFiles(root).Length);

        DirectoryInfo info = new DirectoryInfo(root);
        Console.WriteLine(info.Name);
        File.WriteAllText(Path.Combine(root, "a.txt"), "hi");
        Console.WriteLine(info.GetFiles().Length);

        info.Delete(true);
        Console.WriteLine(Directory.Exists(root));
    }
}

How It Works

  1. Static style. Directory.Exists(root) and Directory.GetFiles(root) pass the path every time. There is no object to create.
  2. Object style. new DirectoryInfo(root) stores the path once. After that, info.Name and info.GetFiles() need no argument.
  3. Both see the same disk. After a.txt is written, info.GetFiles().Length is 1, and the folder is the same one the static calls looked at.
  4. Delete from either side. info.Delete(true) removes the folder with its contents, and the static Directory.Exists prints False. For one or two quick calls, use Directory. For many calls on one folder, DirectoryInfo reads better.

Example 3 — Path Builds the Text, Directory Touches the Disk

Build a file path with Path first, see that nothing exists yet, then let Directory and File make it real.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-hub-path");
        if (Directory.Exists(root)) Directory.Delete(root, true);

        string report = Path.Combine(root, "reports", "2026.txt");
        Console.WriteLine(Path.GetFileName(report));
        Console.WriteLine(Path.GetFileName(Path.GetDirectoryName(report)));
        Console.WriteLine(Directory.Exists(root));

        Directory.CreateDirectory(Path.GetDirectoryName(report));
        File.WriteAllText(report, "ok");
        Console.WriteLine(Directory.Exists(root));
        Console.WriteLine(File.Exists(report));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Only text so far. Path.Combine joins the pieces with the right separator for your system. Nothing is created yet.
  2. Take the text apart. GetFileName gives 2026.txt, and GetDirectoryName then GetFileName gives reports. Both work without a real file.
  3. The disk is still empty. Directory.Exists(root) prints False, which proves that a Path method never touches the disk.
  4. Directory makes it real. CreateDirectory builds root and reports in one call, and File.WriteAllText adds the file. The final checks print True.

Next: C# Directory Methods Index

After you know what the Directory class is for, open the method index for a full tutorial on each Directory API.

Start withWhy
CreateDirectory()Make a folder and all of its parents
Exists()Check whether a folder is there
GetFiles()List the files in a folder, with a pattern such as *.txt
Delete()Remove a folder, and learn the recursive warning

Full list: C# Directory Methods index (/c-sharp/directory/method).

Frequently Asked Questions

Directory is a static class in System.IO. It has methods to create, delete, move, check, and list folders, and to read the working directory, the parent of a path, the root of a path, and the drives of the computer.
Directory is static: you pass the path each time. DirectoryInfo is an object created with new DirectoryInfo(path) that remembers one folder and gives properties such as Name, Parent, and Exists.
Path only works with text: it joins, splits, and reads parts of a path, and never touches the disk. Directory talks to the disk: it creates, deletes, and lists real folders.
Yes. They use a demo folder inside Path.GetTempPath(), create everything first, and delete only what they made. Never copy the recursive Delete(path, true) call onto a real folder.
Open the C# Directory Methods index. It lists CreateDirectory, Delete, Move, Exists, the Get and Enumerate list methods, GetCurrentDirectory, SetCurrentDirectory, GetDirectoryRoot, GetParent, and GetLogicalDrives.

Browse every Directory method

Open the methods index for categorized guides with examples and FAQs.

Directory Methods Index →

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.

8 people found this page helpful