Working with Directories
Directory operations live on IAbsoluteDirectoryPath. Like files, relative directory paths can describe a location but cannot perform I/O; combine them with an absolute base directory first. This article covers existence checks, creation, deletion, moving, flushing to disk and the small set of directory-specific properties, while enumeration lives in Searching and Enumeration.
Existence Checks
Same model as files: IAbsolutePath.Exists for a quick boolean, IAbsolutePath.State for a richer answer:
if (!dir.Exists)
dir.Create();
switch (dir.State)
{
case EntryState.Exists: /* good to go */ break;
case EntryState.ParentExists: /* parent is there, can create */ break;
case EntryState.ParentDoesNotExist: /* Create() will still work */ break;
case EntryState.WrongType: /* a file is here */ break;
}
See the table in Working with Files for the full meaning of each EntryState value.
Creating Directories
Create creates the directory and any missing parent directories. It is idempotent; calling it on an already-existing directory does nothing.
IAbsoluteDirectoryPath logsDir = appBase.CombineDirectory("logs/2026/04");
logsDir.Create(); // creates logs, 2026 and 04 as needed
Tip
Before writing a file at a new location, the simplest pattern is to call Create on the file's IAbsoluteFilePath.ParentDirectory. That single call ensures every directory in the chain exists.
Deleting Directories
Delete has the following signature:
void Delete(bool recursive = false, bool ignoreNotFound = true);
Default behavior:
- Non-recursive (the directory must be empty, otherwise IOException is thrown).
- Missing directories are ignored silently.
dir.Delete(); // empty directories only
dir.Delete(recursive: true); // remove children too
dir.Delete(ignoreNotFound: false); // throw if absent
dir.Delete(recursive: true, ignoreNotFound: false);
Caution
recursive: true is destructive: every file and subdirectory below the path is removed. Confirm the path is what you expect before calling it on user-influenced input.
Moving Directories
Use MoveTo to rename or relocate a directory:
dir.MoveTo(targetDir);
The argument is the new path of the directory itself, not its parent. The destination must be on the same volume and must not already exist; otherwise IOException is thrown. To move across volumes, enumerate the contents and copy them instead (see the Mirror a Structure pattern below).
Flushing to Disk
Creating, renaming, moving or deleting a file or directory changes the entries of the directory it is in, and the operating system writes those changes to the disk some time later, as it does with file data. FlushToDisk forces the entries of an existing directory onto the disk, so that what was created in, renamed into or removed from it survives a power loss or an operating system crash:
IAbsoluteFilePath file = dir.CombineFile("journal.dat");
using (FileStream s = file.OpenStream(FileMode.CreateNew, FileAccess.Write))
{
s.Write(data);
s.Flush(flushToDisk: true); // the contents
}
dir.FlushToDisk(); // the name
Only the directory's own entries are flushed: not the contents of the files in it (see Flushing to Disk in Working with Files), and not the entries of its subdirectories or of its parent directory. A directory's own name is an entry of its parent directory.
On Unix
The directory is opened and flushed with fsync. On Apple platforms F_FULLFSYNC is tried first, as it also flushes the drive's write cache, and fsync is used if it fails.
Some file systems, network mounts in particular, refuse to flush a directory. That is tolerated: if fsync fails with EINVAL, EBADF or ENOTSUP, the call returns normally without anything having been flushed, and the entries are then only as durable as that file system makes them by itself. Any other failure throws IOException.
On Windows
Nothing is flushed, and the call only checks that the directory exists (see Missing Directories for skipping that check). NTFS records changes to directories in its log, and flushing any file on the volume forces that log to disk. A change to a directory is therefore durable once a file on the same volume has been flushed after the change was made. Windows file systems that keep no such log, such as FAT32 and exFAT, give no equivalent guarantee.
Important
On Windows the file flush has to come after the change to the directory. Flushing a file and then renaming it leaves the rename unflushed there, no matter which directories are flushed afterwards.
Durable on Both
Flush a file after the change to its directory, then flush the directory. For a file that is written under a temporary name and then renamed:
tempFile.FlushToDisk(); // the contents, before the file gets its final name
tempFile.MoveTo(finalFile, overwrite: true);
finalFile.FlushToDisk(); // Windows: forces the rename to disk
finalFile.ParentDirectory.FlushToDisk(); // Unix: flushes the entries of the directory
Missing Directories
A directory that does not exist throws DirectoryNotFoundException by default. That differs from Delete on purpose: if the directory is missing then nothing was made durable, which is usually a mistake. Pass ignoreNotFound: true where a missing directory is expected:
dir.FlushToDisk(ignoreNotFound: true);
On Unix, what is ignored is the same as what Delete ignores: a directory that does not exist in a parent directory that does. A parent directory that does not exist still throws DirectoryNotFoundException, and a path that points to a file still throws IOException.
On Windows, the existence of the directory is all that a flush checks, so with ignoreNotFound: true nothing is checked and no system call is made.
Note
Because nothing is checked on Windows with ignoreNotFound: true, a parent directory that does not exist and a path that points to a file pass there as well, where they throw on Unix.
Useful Properties
IsRoot
IsRoot is true for a root directory (C:\, /, a UNC share root). Roots have no parent, so IPath.HasParentDirectory is false.
file.RootDirectory.IsRoot; // true
IsEmpty
IsEmpty returns true if the directory exists and contains no entries:
if (dir.Exists && dir.IsEmpty)
dir.Delete();
IsEmpty requires the directory to exist; otherwise the underlying API throws.
Attributes and Timestamps
Inherited from IAbsolutePath. Same surface as files:
dir.Attributes;
dir.CreationTimeUtc;
dir.LastWriteTimeUtc = DateTime.UtcNow;
For a single consistent snapshot, call GetInfo to obtain a CachedDirectoryInfo. See Cached Entry Info.
Common Patterns
Clear a Directory
dir.Delete(recursive: true);
dir.Create();
This is the simplest reliable way to start from an empty directory. It avoids enumerate-and-delete races.
Mirror a Structure
foreach (IRelativeFilePath rel in source.GetRelativeChildFiles("*", new SearchOptions { Recursive = true }))
{
IAbsoluteFilePath dest = target + rel;
dest.ParentDirectory.Create();
(source + rel).CopyTo(dest, overwrite: true);
}
See Searching and Enumeration for the full enumeration surface.
Prune Old Files
foreach (IAbsoluteFilePath log in dir.GetChildFiles("*.log"))
{
if (log.LastWriteTimeUtc < DateTime.UtcNow.AddDays(-7))
log.Delete();
}
Disk Space and Drive Type
Every absolute directory exposes disk-space information for the volume it lives on via AvailableFreeSpace, TotalFreeSpace, TotalSize, DriveType and FileSystem:
dir.AvailableFreeSpace;
dir.TotalFreeSpace;
dir.TotalSize;
dir.DriveType;
dir.FileSystem; // e.g. "NTFS"
See Drive and Disk Information.
Next Steps
These guides extend what you can do with a directory:
- Searching and Enumeration: query the contents of a directory.
- Cached Entry Info: work with consistent metadata snapshots.
- Drive and Disk Information: pre-flight free-space checks and drive metadata.