C# – Read a text file line by line

There are two simple ways to read a text file line by line: Here’s an example of using File.ReadLines() to read a file line by line in a loop: This outputs the file’s lines: Here’s an example of using File.ReadAllLines() to read the same file into a string array and then loop through it. Skip … Read more

C# – How to read a text file

The simplest way to read a text file is by using a high-level method in the .NET File API (in System.IO), such as File.ReadAllText(). These high-level methods abstract away the details of opening a file stream, reading it with StreamReader, and closing the file. Here’s an example of reading a text file’s content into a … Read more

C# – Save a list of strings to a file

The simplest way to save a list of strings to a file is to use File.WriteAllLines(). This creates (or overwrites) the file and writes each string on a new line. The resulting file looks like this: Note: Showing non-printable newline characters \r\n for clarity. Specifying the separator character What if you want to separate each … Read more