Recall & Review
beginner
What is the simplest way to write text to a file in C#?
Use
File.WriteAllText(path, content) to write a string directly to a file. It creates the file if it doesn't exist or overwrites it if it does.Click to reveal answer
beginner
How do you append text to an existing file in C#?
Use
File.AppendAllText(path, content) to add text at the end of the file without deleting existing content.Click to reveal answer
beginner
What namespace must you include to work with file writing in C#?
Include
using System.IO; to access file writing classes and methods like File.Click to reveal answer
intermediate
How can you write multiple lines to a file efficiently?
Use
File.WriteAllLines(path, string[] lines) to write an array of strings, each as a new line in the file.Click to reveal answer
intermediate
What happens if the file path does not exist when writing a file?
If the directory path does not exist,
File.WriteAllText throws an exception. You must create directories first using Directory.CreateDirectory.Click to reveal answer
Which method overwrites the entire file content when writing text in C#?
✗ Incorrect
File.WriteAllText replaces the whole file content with new text.
What does
File.AppendAllText do?✗ Incorrect
It adds new text at the end without removing existing content.
Which namespace is required to use
File.WriteAllText?✗ Incorrect
System.IO contains file handling classes.
How do you write multiple lines to a file at once?
✗ Incorrect
File.WriteAllLines writes each string in an array as a separate line.
What must you do if the directory does not exist before writing a file?
✗ Incorrect
You must create the directory first or writing will fail.
Explain how to write text to a file in C# and what happens if the file already exists.
Think about the simplest method to write text and its behavior with existing files.
You got /3 concepts.
Describe how to safely write text to a file when the directory might not exist.
Consider what happens if the folder path is missing.
You got /3 concepts.