File System File System
File System File System
DocFX + Singulink = ♥

Search Results for

    Working with Directories

    Overview

    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, creation, deletion and the small set of directory-specific properties. Enumeration and searching live in their own article. See Searching and Enumeration.

    Existence Checks

    Same model as files: Exists for a quick boolean, 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 file.ParentDirectory.Create(), a single call that ensures every directory in the chain exists.

    Deleting Directories

    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.

    Useful Properties

    IsRoot

    true for a root directory (C:\, /, a UNC share root). Roots have no parent; HasParentDirectory is false.

    file.RootDirectory.IsRoot;   // true
    

    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.

    Preserve only certain content

    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:

    dir.AvailableFreeSpace;
    dir.TotalFreeSpace;
    dir.TotalSize;
    dir.DriveType;
    dir.FileSystem;   // e.g. "NTFS"
    

    See Drive and Disk Information.

    Next Steps

    • 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.
    © Singulink. All rights reserved.