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

Search Results for

    Special Locations

    Overview

    Most applications need to resolve a handful of well-known paths: the application base, the current working directory, the temp folder, OS-defined special folders (Documents, AppData, …) and the location of a loaded assembly. The library exposes these as static helpers on DirectoryPath and FilePath that return strongly-typed, ready-to-use absolute paths.

    Note

    Every special-location helper parses the underlying OS string with PathOptions.None so that whatever the operating system returns is accepted verbatim, even if it contains characters or names that would be rejected with NoUnfriendlyNames.

    Application Base

    DirectoryPath.GetAppBase() returns the directory the runtime probes for assemblies. For most apps this is where the executable lives:

    IAbsoluteDirectoryPath baseDir = DirectoryPath.GetAppBase();
    IAbsoluteFilePath bundledData = baseDir.CombineFile("Resources/defaults.json");
    
    Tip

    Prefer GetAppBase() over GetCurrent() for resolving files that ship with your application. The current working directory can change at runtime; the app base does not.

    Current Working Directory

    IAbsoluteDirectoryPath cwd = DirectoryPath.GetCurrent();
    DirectoryPath.SetCurrent(cwd.ParentDirectory!);
    

    SetCurrent requires a path whose PathFormat matches PathFormat.Current.

    Temporary Files and Directories

    GetTemp

    Returns the user's temporary directory (the equivalent of Path.GetTempPath()):

    IAbsoluteDirectoryPath tempDir = DirectoryPath.GetTemp();
    

    CreateTempFile

    Creates a uniquely named, zero-byte temporary file and returns its path. The file already exists when this returns:

    IAbsoluteFilePath workFile = FilePath.CreateTempFile();
    
    try
    {
        using FileStream s = workFile.OpenStream(FileMode.Truncate, FileAccess.Write);
        s.Write(payload);
    }
    finally
    {
        workFile.Delete();
    }
    
    Important

    CreateTempFile actually creates the file; there's no race window. Always make sure something deletes it later, even on the failure path.

    OS Special Folders

    DirectoryPath.GetSpecialFolder(Environment.SpecialFolder) resolves any of the Environment.SpecialFolder values:

    IAbsoluteDirectoryPath appData = DirectoryPath.GetSpecialFolder(Environment.SpecialFolder.ApplicationData);
    IAbsoluteDirectoryPath docs    = DirectoryPath.GetSpecialFolder(Environment.SpecialFolder.MyDocuments);
    
    IAbsoluteDirectoryPath profileDir = appData.CombineDirectory("MyApp");
    profileDir.Create();
    

    Some special folders may not be defined on every platform; in that case the underlying API returns an empty string and the helper throws.

    Assembly Locations

    Resolve the file path or directory of a loaded assembly:

    IAbsoluteFilePath thisDll = FilePath.GetAssemblyLocation(typeof(MyType).Assembly);
    IAbsoluteDirectoryPath thisDir = DirectoryPath.GetAssemblyLocation(typeof(MyType).Assembly);
    
    Caution

    Assembly location is unavailable when an app is published as a single file. The helpers throw InvalidOperationException in that case; use GetAppBase() instead for resources that ship with your app.

    Mounting Points

    DirectoryPath.GetMountingPoints() returns the file system roots (drives on Windows, mount points on Unix):

    foreach (IAbsoluteDirectoryPath mount in DirectoryPath.GetMountingPoints())
    {
        Console.WriteLine($"{mount.PathDisplay}  {mount.DriveType}  {mount.AvailableFreeSpace:N0} bytes free");
    }
    

    See Drive and Disk Information for the full set of disk-space members available on every absolute directory.

    Next Steps

    • Working with Files and Working with Directories: what to do once you have a path.
    • Drive and Disk Information: query disk space and drive type from any absolute directory.
    © Singulink. All rights reserved.