C# Directory Methods

About This Index
This page is the C# Directory Methods index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs. All examples use a small demo folder inside Path.GetTempPath(), remove it at the end, and print only leaf names or true and false facts, so the output you see is the output you get.
Learn what the Directory class is for on /c-sharp/directory. Use this page when you need a specific API such as CreateDirectory(), GetFiles(), or Exists().
The Get and Enumerate methods return paths in the order the file system chooses. Sort the names (for example with Array.Sort(items, StringComparer.Ordinal)) before you print or compare them. Also be careful with Delete(path, true): it removes a folder and everything inside it, with no undo.
How to Call a Directory Method
Every Directory method is static, so you call it on the class name. Add using System.IO; at the top of the file:
string folder = Path.Combine(Path.GetTempPath(), "codetofun-demo");
Directory.CreateDirectory(folder); // make it (safe to repeat)
bool there = Directory.Exists(folder); // check it
string[] files = Directory.GetFiles(folder, "*.txt"); // list files into an array
foreach (string d in Directory.EnumerateDirectories(folder)) { } // stream subfolders
Directory.Delete(folder, true); // remove it and its contents C# Directory Methods Table
All method guides in this C# tutorial series (15 tutorials), grouped by purpose.
Create / delete / move
3 methodsMake new folders, remove folders, and move or rename a folder. Delete asks for the recursive option before it removes a folder that has contents.
Check
1 methodAsk whether a folder is really there. Exists never throws; it returns false for files, bad paths, and no permission.
List (arrays)
3 methodsRead subfolders, files, or both into a string array. You get Length and indexes, but the whole array is built before the call returns.
| Method | Description | Tutorial |
|---|---|---|
GetDirectories() | Get the paths of subfolders as a string array you can sort, count, and index. | Try it |
GetFiles() | Get the paths of files as a string array, with an optional pattern such as *.txt. | Try it |
GetFileSystemEntries() | Get files and subfolders together as one string array. | Try it |
List (lazy)
3 methodsStream paths one at a time while the folder is read. Best for huge folders, or when you may stop early.
| Method | Description | Tutorial |
|---|---|---|
EnumerateDirectories() | Stream subfolder paths one at a time without loading them all first. | Try it |
EnumerateFiles() | Stream file paths one at a time, with an optional pattern such as *.txt. | Try it |
EnumerateFileSystemEntries() | Stream files and subfolders together in one lazy sequence. | Try it |
Paths / location
5 methodsRead or change the working directory, find the root and parent of a path, and list the drives of the computer.
| Method | Description | Tutorial |
|---|---|---|
GetCurrentDirectory() | Read the folder the program is working in; relative paths start from here. | Try it |
SetCurrentDirectory() | Change the working directory for the whole program; relative paths follow it. | Try it |
GetDirectoryRoot() | Get the root of a path, such as C:\ or /, without touching the disk. | Try it |
GetParent() | Get the parent folder of a path as a DirectoryInfo; null for a root. | Try it |
GetLogicalDrives() | List the drive roots on this computer, such as C:\ and D:\. | Try it |
Frequently Asked Questions
Start with CreateDirectory()
Make your first folder, including every missing parent, in one safe call.
12 people found this page helpful
