Skip to content

File I/O

Overview

The File module provides functions for reading, writing, and managing files on disk.

Convenience Methods

Read or write an entire file in one call:

string text = File.ReadAllText("data.txt");
int8[] bytes = File.ReadAllBytes("image.png");
string[] lines = File.ReadAllLines("data.txt");
File.WriteAllText("output.txt", "Hello, file!\n");
File.WriteAllBytes("header.bin", bytes);
File.AppendAllText("log.txt", "New entry\n");

Temporary Files

File temp = File.CreateTemp();
temp.WriteString("temporary data");
Console.WriteLine(temp.Path);
temp.Close();

Stream-Based I/O

For large files, use File.Open with instance methods:

File file = File.Open("large.txt", "r");
while (file.IsOpen)
{
string line = file.ReadLine();
if (line == "")
{
break;
}
Console.WriteLine(line);
}
file.Close();

Instance Methods

MethodDescription
file.Read(count)Read count bytes
file.ReadLine()Read one line
file.Write(bytes)Write raw bytes
file.WriteString(s)Write a string
file.WriteLine(s)Write a string with newline
file.Seek(pos)Seek to byte position
file.Flush()Flush buffered writes
file.Close()Close the file

Instance Properties

PropertyTypeDescription
file.PositionintCurrent byte offset
file.LengthintTotal file size
file.CanReadboolOpened for reading
file.CanWriteboolOpened for writing
file.IsOpenboolFile is still open
file.PathstringFile path

File Locking

Lock files to prevent concurrent access:

File f = File.Open("data.bin", "rw");
f.Lock(); // exclusive lock (entire file)
// ... write safely ...
f.Unlock();
f.LockShared(); // shared/read lock
// ... read safely ...
f.Unlock();
f.LockRange(0, 1024); // lock a byte range
f.Unlock();
f.Close();
MethodDescription
file.Lock()Exclusive lock on the entire file
file.LockRange(offset, length)Lock a specific byte range
file.LockShared()Shared (read) lock
file.Unlock()Release the lock

The file.IsLocked property indicates whether the file is currently locked.

File Operations

File.Exists("config.json");
File.Delete("temp.txt");
File.Copy("source.txt", "dest.txt");
File.Move("old.txt", "new.txt");

Directory Operations

Directory.Exists("./build");
Directory.Create("./output");
Directory.CreateAll("./a/b/c");
Directory.Delete("./temp");
string[] files = Directory.GetFiles("./data");
string[] dirs = Directory.GetDirectories("./src");

Path Utilities

string full = Path.Combine("./dir", "file.txt");
string name = Path.GetFileName("./dir/file.txt"); // "file.txt"
string dir = Path.GetDirectory("./dir/file.txt"); // "./dir"
string ext = Path.GetExtension("report.pdf"); // ".pdf"
string abs = Path.GetFullPath("./relative"); // absolute path

FileWatcher

Monitor the file system for changes:

FileWatcher watcher = FileWatcher.Create("./data", FileChangeType.All);
watcher.Start();
// Non-blocking check
bool changed = watcher.PollChange();
// Blocking wait
watcher.WaitForChange();
// Wait with timeout (milliseconds)
watcher.WaitForChangeTimeout(5000);
watcher.Stop();
MethodDescription
FileWatcher.Create(path, filter)Create a watcher with a change type filter
watcher.Start()Begin watching
watcher.Stop()Stop watching
watcher.PollChange()Non-blocking check for changes
watcher.WaitForChange()Block until a change occurs
watcher.WaitForChangeTimeout(ms)Block with a timeout
PropertyTypeDescription
watcher.PathstringWatched path
watcher.FilterintFileChangeType filter
watcher.IsWatchingboolWhether currently active