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

File class static methods in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Using File Class Static Methods in C#
📖 Scenario: You are working on a simple program that manages a text file storing a list of favorite fruits. You will create the file, add some fruits, read the contents, and finally delete the file using C# File class static methods.
🎯 Goal: Build a C# program that creates a text file called fruits.txt, writes a list of fruits into it, reads and displays the fruits, and then deletes the file.
📋 What You'll Learn
Create a string array called fruits with the exact values: "Apple", "Banana", "Cherry"
Create a string variable called filePath with the exact value "fruits.txt"
Use File.WriteAllLines to write the fruits array to the file at filePath
Use File.ReadAllLines to read the contents of the file into a string array called readFruits
Use a foreach loop with variable fruit to print each fruit from readFruits
Use File.Delete to delete the file at filePath
💡 Why This Matters
🌍 Real World
Managing text files is common in many applications, such as saving user data, logs, or simple databases.
💼 Career
Knowing how to use file operations with the File class is essential for software developers working with data storage and file management.
Progress0 / 4 steps
1
Create the fruits array
Create a string array called fruits with these exact values: "Apple", "Banana", "Cherry".
C Sharp (C#)
Need a hint?

Use curly braces {} to list the fruits inside the array.

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 double quotes to set the file path string.

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

Remember to include using System.IO; at the top to use the File class.

4
Read and display the fruits, then delete the file
Use File.ReadAllLines with filePath to read the file into a string array called readFruits. Then use a foreach loop with variable fruit to print each fruit. Finally, use File.Delete with filePath to delete the file.
C Sharp (C#)
Need a hint?

Use Console.WriteLine(fruit); inside the loop to print each fruit on its own line.