Directory
Source: lib/Directory.cs
This page is generated from the source by scripts/gen-reference.py.
Directories: make, delete, move and list them, and the current directory.
FileError error = Directory.Create("logs/2026/11"); // and its parents
foreach (DirectoryEntry entry in Directory.Enumerate("logs"))
Out.Line(entry.Name, entry.IsDirectory ? "/" : "");
Directory.Delete("logs", recursive: true);
Linux, macOS, Windows and WebAssembly (see lib/FileSystem.cs). Failures come back as a FileError rather than ending the program.
Functions on directories, and the program's current directory.
Members
Exists
public static bool Exists(ReadOnlySpan<char> path)
Whether a directory exists at the path (following symbolic links).
Create
public static FileError Create(ReadOnlySpan<char> path)
Creates the directory, and any directories on the way to it that don't exist yet. A directory that's already there is fine (None); a file with the name is AlreadyExists, and a file on the way is NotADirectory.
Delete
public static FileError Delete(ReadOnlySpan<char> path, bool recursive = false)
Deletes the directory. It must be empty (NotEmpty otherwise) unless recursive, which deletes everything in it first. Symbolic links inside are deleted, not followed; a path that is itself a symbolic link to a directory deletes only the link.
Move
public static FileError Move(ReadOnlySpan<char> from, ReadOnlySpan<char> to)
Moves (or renames) the directory. AlreadyExists if something has the new name. Other if the two are on different file systems, which the system can't move a directory between.
GetCurrentDirectory
public static string GetCurrentDirectory()
The current directory, as an absolute path; an empty string if the system won't say. Relative paths start from here.
SetCurrentDirectory
public static FileError SetCurrentDirectory(ReadOnlySpan<char> path)
Makes the path the current directory.
Enumerate
public static DirectoryEntries Enumerate(ReadOnlySpan<char> path)
The entries in the directory, for foreach: each one's name and kind, in the order the system gives them, without "." and "..". If the directory can't be read, Error says why and there are no entries. Enumerate it once; foreach closes it at the end, as does Dispose if it isn't enumerated.
DirectoryEntries
The entries of a directory, from Directory.Enumerate, for foreach. Error says why the directory couldn't be read, or None.
Error
public FileError Error
Why the directory couldn't be opened or read to the end, or None.
GetEnumerator
public DirectoryEntries GetEnumerator()
For foreach. The enumerator takes the open directory over, so this copy no longer needs disposing.
Current
public DirectoryEntry Current
The entry MoveNext moved to. Its Name is good until the next MoveNext.
MoveNext
public bool MoveNext()
Moves to the next entry; false when there are no more.
Dispose
public void Dispose()
Closes the directory. foreach calls it; call it yourself only if you don't enumerate.
DirectoryEntry
An entry in a directory: its name, and what kind of entry it is.
Name
public ReadOnlySpan<char> Name
The entry's name, without the directory's path. Good until the next entry.
IsDirectory
public bool IsDirectory
Whether it's a directory. A symbolic link to one is IsSymbolicLink instead.
IsFile
public bool IsFile
Whether it's an ordinary file.
IsSymbolicLink
public bool IsSymbolicLink
Whether it's a symbolic link (on Windows, also a junction).