C# can be used to read files. You can either read a file directly into a string variable or read a file line by line.

These functionality C# provides out of the box is for read files on the hard disk, not on the cloud.

Read files in C#

Read file

The c# program below reads a file from the disk. C# will read the file from the same directory as your program. If the file is in another directory, specify its path.

If you want to read a file at once, you can use:

string contents = File.ReadAllText(@"test.txt");

This reads the entire file into a c# string.

Line by line

If you want to read a file line by line, into an array, you can use this code:

var lines = File.ReadAllLines(fileName);
foreach (var line in lines) {
// process line, add to array?


  1. Think of when you’d read a file ‘line by line’ vs ‘at once’?
  2. Create a new file containing names and read it into an array