C# › File I/O & Streams

How do you read and write an entire text file's contents using the simplest File class methods?

Beginner

Answer

File.ReadAllText() reads an entire file's contents into a single string; File.WriteAllText() writes a string to a file, creating it if it doesn't exist or overwriting it if it does — both are synchronous and load the ENTIRE file into memory at once, fine for small files.
string content = File.ReadAllText("data.txt");
File.WriteAllText("output.txt", "Hello, World!");

// Async equivalents
string content2 = await File.ReadAllTextAsync("data.txt");
await File.WriteAllTextAsync("output.txt", "Hello, World!");
Real-world example Reading a small configuration file's entire content in one call, without manually managing a stream.

Common follow-ups: Why should you prefer the Async versions of these methods in a server application?