Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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#)
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#)
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#)
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#)
Hint
Use Console.WriteLine(fruit); inside the loop to print each fruit on its own line.
Practice
(1/5)
1. Which of the following File class static methods checks if a file exists at a given path?
easy
A. File.Exists(path)
B. File.ReadAllText(path)
C. File.Delete(path)
D. File.Copy(source, destination)
Solution
Step 1: Understand the purpose of each method
File.Exists(path) checks if the file is present. File.ReadAllText(path) reads file content. File.Delete(path) deletes a file. File.Copy(source, destination) copies a file.
Step 2: Identify the method that checks existence
The method that returns a boolean indicating if the file exists is File.Exists(path).
Final Answer:
File.Exists(path) -> Option A
Quick Check:
Check file existence = File.Exists(path) [OK]
Hint: Exists method returns true if file is present [OK]
Common Mistakes:
Confusing ReadAllText with Exists
Using Delete to check existence
Thinking Copy checks file presence
2. Which of the following is the correct syntax to write text to a file using the File class?
easy
A. File.WriteText(path, "Hello World");
B. File.WriteAllText(path, "Hello World");
C. File.Write(path, "Hello World");
D. File.WriteLine(path, "Hello World");
Solution
Step 1: Recall the correct method name for writing text
The File class uses WriteAllText to write all text to a file at once.
Step 2: Check method signatures
WriteText, Write, and WriteLine are not valid static methods of File class.
Final Answer:
File.WriteAllText(path, "Hello World") -> Option B
Quick Check:
Write text to file = WriteAllText [OK]
Hint: Use WriteAllText to write full text at once [OK]
Common Mistakes:
Using non-existent WriteText method
Confusing File class with StreamWriter methods
Using WriteLine which is not in File class
3. What will be the output of the following code if the file "test.txt" contains the text "Hello"?
B. File.Delete should be called before the first File.Copy
C. File.Copy cannot copy files with .txt extension
D. Second File.Copy will throw an exception because source.txt was deleted
Solution
Step 1: Trace the file operations
First, source.txt is copied to dest.txt. Then source.txt is deleted. Finally, the code tries to copy source.txt again.
Step 2: Identify the problem
After deletion, source.txt no longer exists, so the second copy call will throw a FileNotFoundException.
Final Answer:
Second File.Copy will throw an exception because source.txt was deleted -> Option D
Quick Check:
Copy after delete causes error [OK]
Hint: Cannot copy a file after deleting it [OK]
Common Mistakes:
Assuming File.Copy works on deleted files
Thinking order of Delete and Copy doesn't matter
Believing .txt files cannot be copied
5. You want to create a backup of a file only if it exists, without overwriting an existing backup. Which code snippet correctly uses File class static methods to do this?
hard
A. if (File.Exists("file.txt")) File.Copy("file.txt", "backup.txt", true);
B. File.Copy("file.txt", "backup.txt");
C. if (File.Exists("file.txt") && !File.Exists("backup.txt")) File.Copy("file.txt", "backup.txt");
D. File.Copy("file.txt", "backup.txt", false);
Solution
Step 1: Understand the requirements
Backup only if original file exists and do not overwrite existing backup file.
Step 2: Analyze each option
if (File.Exists("file.txt")) File.Copy("file.txt", "backup.txt", true); overwrites backup.txt because of 'true' overwrite flag. if (File.Exists("file.txt") && !File.Exists("backup.txt")) File.Copy("file.txt", "backup.txt"); checks existence of both files and copies only if backup.txt does not exist. File.Copy("file.txt", "backup.txt"); copies without checks, risking errors or overwrites. File.Copy("file.txt", "backup.txt", false); copies without overwrite but does not check if original file exists.
Final Answer:
if (File.Exists("file.txt") && !File.Exists("backup.txt")) File.Copy("file.txt", "backup.txt"); -> Option C
Quick Check:
Check both files before copy to avoid overwrite [OK]
Hint: Check both files exist before copying without overwrite [OK]