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 filePathUse
File.ReadAllLines to read the contents of the file into a string array called readFruitsUse a
foreach loop with variable fruit to print each fruit from readFruitsUse
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