C# Language File and Stream I/O Reading from a file using the System.IO.File class

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

You can use the System.IO.File.ReadAllText function to read the entire contents of a file into a string.

string text = System.IO.File.ReadAllText(@"C:\MyFolder\MyTextFile.txt");

You can also read a file as an array of lines using the System.IO.File.ReadAllLines function:

string[] lines = System.IO.File.ReadAllLines(@"C:\MyFolder\MyTextFile.txt");


Got any C# Language Question?