File

Source: lib/File.cs

This page is generated from the source by scripts/gen-reference.py.

Files: read or write a whole file in one call, or open one and read, write and seek in it.

File.WriteAllText("notes.txt", "first line\n");
if (File.TryReadAllText(arena, "notes.txt", out string text)) ...
File log = File.OpenAppend("log.txt");
if (log.IsOpen)
{
    log.Write("started\n");
    unsafe { log.Close(); } // SAFETY: log isn't used after this.
}

Text is UTF-8 on disk and UTF-16 in a string, as with Out and In. Paths are passed to the system as UTF-8. Nothing here fails the program: a file that can't be opened, read or written shows up as a false or a 0, or, from the functions that return one, a FileError saying why.

Copy, Move, Remove, GetInfo, CreateNew and the FileError forms of the open functions work on Linux, macOS, Windows and WebAssembly; they use the calls lib/FileSystem.cs describes. The rest works everywhere.

Underneath are the C library's stdio functions (fopen, fread, fwrite, fseeko, ftello, fclose, remove): the system's own on Linux, macOS, Windows and WebAssembly; dos/runtime.c and efi/machine.c supply them on DOS and UEFI. On Windows, win/runtime.c's fopen and remove take the UTF-8 path in place of the Universal CRT's, which would read it in the ANSI code page. On DOS, names are 8.3. Under wasmtime, a program sees only the directories it is given with --dir.

An open file, or a file that couldn't be opened (IsOpen is false). A handle: copies share the file. Also the functions that read or write a whole file at once.

Members

OpenRead

public static File OpenRead(ReadOnlySpan<char> path)

Opens an existing file for reading. IsOpen is false if it can't be opened.

Create

public static File Create(ReadOnlySpan<char> path)

Creates a file for writing, emptying it if it exists. IsOpen is false if it can't be.

OpenAppend

public static File OpenAppend(ReadOnlySpan<char> path)

Opens a file for writing at its end, creating it if needed. IsOpen is false if it can't be.

OpenRead

public static File OpenRead(ReadOnlySpan<char> path, out FileError error)

OpenRead, saying why it failed: NotFound, AccessDenied, IsADirectory...

Create

public static File Create(ReadOnlySpan<char> path, out FileError error)

Create, saying why it failed.

CreateNew

public static File CreateNew(ReadOnlySpan<char> path, out FileError error)

Creates a new file for writing, failing with AlreadyExists if anything has the name already: no other program can create it in between.

OpenAppend

public static File OpenAppend(ReadOnlySpan<char> path, out FileError error)

OpenAppend, saying why it failed.

IsOpen

public bool IsOpen

Whether the file was opened.

Read

public int Read(Span<byte> buffer)

Reads up to buffer.Length bytes and returns how many it read: fewer at the end of the file, and 0 once there is nothing left or the file isn't open.

Write

public int Write(ReadOnlySpan<byte> bytes)

Writes bytes as they are and returns how many were written.

public bool Write(ReadOnlySpan<char> text)

Writes text as UTF-8. Returns false if not all of it was written.

public bool Write(string text)

Writes a string as UTF-8. Returns false if not all of it was written.

Position

public long Position

Where the next read or write happens, in bytes from the start; -1 if the file isn't open.

Seek

public bool Seek(long position)

Moves to position bytes from the start. Returns false if it can't.

Length

public long Length

The file's size in bytes, or -1 if it isn't open. The position stays where it was.

Close

public unsafe void Close()

Writes out what is buffered and closes the file. Does nothing if it isn't open.

Safety: Copies of this handle still point at the closed file. Don't use any of them after this, and don't close the file twice.

Exists

public static bool Exists(ReadOnlySpan<char> path)

Whether a file exists at the path: not a directory (following symbolic links). On DOS, UEFI and microcontrollers, whether it can be opened for reading.

GetInfo

public static FileError GetInfo(ReadOnlySpan<char> path, out FileInfo info)

What the system knows about the file or directory: its length, kind and when it was last written (following symbolic links).

Move

public static FileError Move(
    ReadOnlySpan<char> from,
    ReadOnlySpan<char> to,
    bool overwrite = false)

Moves (or renames) the file. With overwrite a file already at to is replaced; without it that's AlreadyExists. Between file systems, the file is copied and then deleted.

Copy

public static FileError Copy(
    ReadOnlySpan<char> from,
    ReadOnlySpan<char> to,
    bool overwrite = false)

Copies the file's bytes to to. With overwrite a file already there is replaced; without it that's AlreadyExists.

Remove

public static FileError Remove(ReadOnlySpan<char> path)

Deletes a file, saying why it couldn't: NotFound, AccessDenied, IsADirectory...

Delete

public static bool Delete(ReadOnlySpan<char> path)

Deletes a file. Returns false if it couldn't, for example because it doesn't exist.

TryReadAllBytes

public static unsafe bool TryReadAllBytes(
    Arena arena,
    ReadOnlySpan<char> path,
    out Span<byte> bytes)

Reads a whole file into arena. Returns false, and an empty span, if the file can't be opened or read, or the arena is out of memory.

Safety: The span points into the arena. Don't use it after the memory is freed.

TryReadAllText

public static bool TryReadAllText(
    Arena arena,
    ReadOnlySpan<char> path,
    out string text)

Reads a whole UTF-8 file into a string in arena. Bytes that aren't UTF-8 become U+FFFD. Returns false, and null, if the file can't be opened or read, or the arena is out of memory.

WriteAllBytes

public static bool WriteAllBytes(
    ReadOnlySpan<char> path,
    ReadOnlySpan<byte> bytes)

Writes bytes to a file, replacing it if it exists. Returns false if that failed.

WriteAllText

public static bool WriteAllText(
    ReadOnlySpan<char> path,
    ReadOnlySpan<char> text)

Writes text to a file as UTF-8, replacing it if it exists. Returns false if that failed.

AppendAllText

public static bool AppendAllText(
    ReadOnlySpan<char> path,
    ReadOnlySpan<char> text)

Adds text to the end of a file as UTF-8, creating the file if needed. Returns false if that failed.

FileInfo

What File.GetInfo says about a file or directory.

Length

public long Length

The file's length in bytes.

IsDirectory

public bool IsDirectory

Whether it's a directory.

LastWriteUnixMilliseconds

public long LastWriteUnixMilliseconds

When it was last written, in milliseconds since 1970-01-01 UTC, as Clock.UnixMilliseconds.