Reading text files lets your program get information stored in files. This helps your program use data saved earlier or from other sources.
Reading text files in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
string[] lines = System.IO.File.ReadAllLines("filename.txt"); // or to read all text as one string string text = System.IO.File.ReadAllText("filename.txt");
ReadAllLines reads the file and returns each line as an array element.
ReadAllText reads the whole file as one big string.
Examples
data.txt and prints each line.C Sharp (C#)
string[] lines = System.IO.File.ReadAllLines("data.txt"); foreach (string line in lines) { Console.WriteLine(line); }
message.txt as one string and prints it.C Sharp (C#)
string text = System.IO.File.ReadAllText("message.txt"); Console.WriteLine(text);
Sample Program
This program reads all lines from a file named example.txt and prints each line to the console.
C Sharp (C#)
using System; class Program { static void Main() { // Assume file 'example.txt' exists with some text string[] lines = System.IO.File.ReadAllLines("example.txt"); Console.WriteLine("Contents of example.txt:"); foreach (string line in lines) { Console.WriteLine(line); } } }
Important Notes
Make sure the file path is correct or you will get an error.
If the file is large, reading all lines at once might use a lot of memory.
You can also read files line by line using streams for better memory use.
Summary
Use File.ReadAllLines to get all lines as an array.
Use File.ReadAllText to get the whole file as one string.
Always check the file path and handle errors in real programs.
Practice
1. What does the
File.ReadAllLines method do in C#?easy
Solution
Step 1: Understand the method purpose
File.ReadAllLinesreads a text file and returns each line as an element in a string array.Step 2: Compare with other methods
File.ReadAllTextreturns the whole file as one string, not an array of lines.Final Answer:
Reads all lines from a text file and returns them as a string array. -> Option BQuick Check:
ReadAllLines = string array [OK]
Hint: ReadAllLines returns array of lines, not one big string [OK]
Common Mistakes:
- Confusing ReadAllLines with ReadAllText
- Thinking it writes to a file
- Assuming it deletes files
2. Which of the following is the correct syntax to read all text from a file named "data.txt" using
File.ReadAllText?easy
Solution
Step 1: Identify method return types
File.ReadAllTextreturns a single string, so the variable must be string.Step 2: Match syntax with variable type
string content = File.ReadAllText("data.txt"); uses string variable with ReadAllText correctly. Options B and D mismatch method and variable types. string[] content = File.ReadAllText("data.txt"); tries to assign string to string array.Final Answer:
string content = File.ReadAllText("data.txt"); -> Option AQuick Check:
ReadAllText returns string, so variable is string [OK]
Hint: Match method return type with variable type [OK]
Common Mistakes:
- Assigning ReadAllText to string array
- Using ReadAllLines but expecting string
- Wrong variable type for method
3. What will be the output of this C# code if the file "example.txt" contains three lines: "Hello", "World", "!"?
string[] lines = File.ReadAllLines("example.txt");
Console.WriteLine(lines.Length);medium
Solution
Step 1: Understand ReadAllLines output
The method returns an array with one element per line in the file. Here, 3 lines means array length is 3.Step 2: Analyze Console.WriteLine output
Printinglines.Lengthoutputs the number of lines, which is 3.Final Answer:
3 -> Option AQuick Check:
Array length = number of lines = 3 [OK]
Hint: ReadAllLines length equals number of lines in file [OK]
Common Mistakes:
- Expecting content printed instead of length
- Confusing ReadAllText with ReadAllLines
- Assuming runtime error without cause
4. Identify the error in this code snippet that reads all lines from "notes.txt":
string[] lines = File.ReadAllLines(notes.txt);
foreach (string line in lines)
{
Console.WriteLine(line);
}medium
Solution
Step 1: Check file path syntax
The file name must be a string literal, so it needs quotes: "notes.txt".Step 2: Verify other code parts
File.ReadAllLinesreturns string array, foreach syntax is correct, and Console.WriteLine can print strings.Final Answer:
Missing quotes around the file name in ReadAllLines. -> Option DQuick Check:
File path must be in quotes [OK]
Hint: File names must be in quotes in method calls [OK]
Common Mistakes:
- Forgetting quotes around file path
- Thinking ReadAllLines returns string
- Misunderstanding foreach syntax
5. You want to read a text file and count how many lines contain the word "error" (case insensitive). Which code snippet correctly does this?
hard
Solution
Step 1: Choose method to read lines
File.ReadAllLinesreturns an array of lines, perfect for line-by-line processing.Step 2: Count lines with "error" case-insensitive
var lines = File.ReadAllLines("log.txt"); int count = lines.Count(line => line.ToLower().Contains("error")); converts each line to lowercase and checks if it contains "error", then counts matches using LINQ.Step 3: Check other options
string content = File.ReadAllText("log.txt"); int count = content.Split('\n').Count(line => line.Contains("error")); uses ReadAllText but misses case-insensitive check. var lines = File.ReadAllLines("log.txt"); int count = 0; foreach(var line in lines) { if(line.Contains("error")) count++; } misses case-insensitive check. string[] lines = File.ReadAllText("log.txt").Split('\n'); int count = lines.Count(line => line.Contains("error")); incorrectly assigns ReadAllText to string array without ToLower.Final Answer:
var lines = File.ReadAllLines("log.txt"); int count = lines.Count(line => line.ToLower().Contains("error")); -> Option CQuick Check:
Use ReadAllLines + ToLower + Count for case-insensitive search [OK]
Hint: Use ReadAllLines and ToLower for case-insensitive line checks [OK]
Common Mistakes:
- Ignoring case sensitivity
- Using ReadAllText but treating as array
- Not converting lines to lowercase
