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