File.ReadAllText
In this page:
File.ReadAllText
File.ReadAllText reads an entire file into a single string in one call, which is the simplest way to load small-to-medium text files. File.WriteAllText is its counterpart for writing. Both are static methods on the System.IO.File class and open, use, and close the file automatically.
Note: For very large files, prefer StreamReader so you can process the file line by line instead of loading it all into memory.
Example: File.ReadAllText
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string path = "greeting.txt";
File.WriteAllText(path, "Hello from File.WriteAllText!");
string content = File.ReadAllText(path);
Console.WriteLine(content);
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: