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

Writing text files in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Writing text files
📖 Scenario: You are creating a simple program to save a list of favorite fruits to a text file on your computer. This is useful when you want to keep a record of items that you can open later.
🎯 Goal: Build a C# program that writes a list of fruit names into a text file called fruits.txt.
📋 What You'll Learn
Create a list of fruit names
Set the file path to fruits.txt
Write the fruit names to the file, each on a new line
Print a confirmation message after writing
💡 Why This Matters
🌍 Real World
Saving lists or logs to text files is common in many applications, like saving user preferences, logs, or simple data records.
💼 Career
Knowing how to write files is important for software developers, especially when working with data storage, configuration files, or generating reports.
Progress0 / 4 steps
1
Create a list of fruits
Create a List<string> called fruits with these exact entries: "Apple", "Banana", "Cherry", "Date", "Elderberry".
C Sharp (C#)
Need a hint?

Use new List<string> { } to create the list with the fruit names inside curly braces.

2
Set the file path
Create a string variable called filePath and set it to the exact value "fruits.txt".
C Sharp (C#)
Need a hint?

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

3
Write the fruits to the file
Use System.IO.File.WriteAllLines with filePath and fruits to write all fruit names to the file.
C Sharp (C#)
Need a hint?

Use File.WriteAllLines(filePath, fruits); to write the list to the file.

4
Print confirmation message
Write a Console.WriteLine statement to print exactly "Fruits have been saved to fruits.txt".
C Sharp (C#)
Need a hint?

Use Console.WriteLine("Fruits have been saved to fruits.txt"); to show the message.