0
0
C Sharp (C#)programming~30 mins

StreamReader and StreamWriter in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Reading and Writing Text Files with StreamReader and StreamWriter
📖 Scenario: You are working on a simple note-taking app. You want to save notes to a file and read them back later.
🎯 Goal: Learn how to use StreamWriter to write text to a file and StreamReader to read text from a file in C#.
📋 What You'll Learn
Create a text file with some notes using StreamWriter
Set the file path in a variable
Read the notes back from the file using StreamReader
Print the notes to the console
💡 Why This Matters
🌍 Real World
Reading and writing text files is common in apps that save user data, logs, or configuration settings.
💼 Career
Many programming jobs require working with files to store and retrieve data, making StreamReader and StreamWriter essential tools.
Progress0 / 4 steps
1
Create a file path variable
Create a string variable called filePath and set it to the exact value "notes.txt".
C Sharp (C#)
Need a hint?

Use string filePath = "notes.txt"; to set the file path.

2
Write notes to the file using StreamWriter
Use StreamWriter with the variable filePath to write these exact lines to the file:
"Shopping list:"
"- Milk"
"- Bread"
Use a using block to open and close the writer.
C Sharp (C#)
Need a hint?

Use using (var writer = new System.IO.StreamWriter(filePath)) and writer.WriteLine() for each line.

3
Read notes from the file using StreamReader
Use StreamReader with the variable filePath inside a using block to read all lines from the file. Store the result in a string variable called notes using ReadToEnd().
C Sharp (C#)
Need a hint?

Use using (var reader = new System.IO.StreamReader(filePath)) and notes = reader.ReadToEnd();.

4
Print the notes to the console
Write a Console.WriteLine statement to print the variable notes.
C Sharp (C#)
Need a hint?

Use Console.WriteLine(notes); to display the notes.