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

Search Results for

    Special Locations

    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, etc.) 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 PathOptions.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 DirectoryPath.GetAppBase over DirectoryPath.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

    Use DirectoryPath.GetCurrent and DirectoryPath.SetCurrent:

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

    DirectoryPath.SetCurrent requires a path whose IPath.PathFormat matches PathFormat.Current.

    Temporary Files and Directories

    GetTemp

    DirectoryPath.GetTemp returns the user's temporary directory (the equivalent of Path.GetTempPath):

    IAbsoluteDirectoryPath tempDir = DirectoryPath.GetTemp();
    

    CreateTempFile

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

    FilePath.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 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 ArgumentException.

    Assembly Locations

    Resolve the file path or directory of a loaded Assembly with FilePath.GetAssemblyLocation or DirectoryPath.GetAssemblyLocation:

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

    With a location in hand, these guides cover what comes next:

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