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
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#?
AFile.WriteAllLines
BFile.AppendAllText
CFile.WriteAllText
DFile.ReadAllText
✗ Incorrect
File.WriteAllText replaces the whole file content with new text.
What does File.AppendAllText do?
ADeletes the file
BAdds text to the end of the file
CReads text from the file
DCreates a new empty file
✗ Incorrect
It adds new text at the end without removing existing content.
Which namespace is required to use File.WriteAllText?
ASystem.IO
BSystem.Net
CSystem.Text
DSystem.Threading
✗ Incorrect
System.IO contains file handling classes.
How do you write multiple lines to a file at once?
AFile.ReadAllLines
BFile.AppendAllText with newline characters
CFile.WriteAllText with newline characters
DFile.WriteAllLines with a string array
✗ 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?
AUse Directory.CreateDirectory to create it
BNothing, it creates automatically
CUse File.Delete first
DUse File.ReadAllText
✗ 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.
Practice
(1/5)
1. What does the File.WriteAllText method do in C#?
easy
A. It creates a new text file or overwrites an existing one with the specified content.
B. It reads all text from a file and returns it as a string.
C. It appends text to the end of an existing file without overwriting.
D. It deletes a specified text file from the disk.
Solution
Step 1: Understand the purpose of File.WriteAllText
This method is designed to write text to a file, creating it if it doesn't exist or overwriting it if it does.
Step 2: Compare with other file methods
Reading text uses File.ReadAllText, appending uses File.AppendAllText, and deleting uses File.Delete, so these are different methods.
Final Answer:
It creates a new text file or overwrites an existing one with the specified content. -> Option A
Quick Check:
File.WriteAllText writes or overwrites text [OK]
Hint: WriteAllText creates or overwrites files with given text [OK]
Common Mistakes:
Confusing WriteAllText with ReadAllText
Thinking it appends instead of overwriting
Assuming it deletes files
2. Which of the following is the correct syntax to write "Hello World" to a file named "greeting.txt" using File.WriteAllText?
easy
A. File.WriteText("greeting.txt", "Hello World");
B. File.WriteAllText("Hello World", "greeting.txt");
C. WriteAllText.File("greeting.txt", "Hello World");
D. File.WriteAllText("greeting.txt", "Hello World");
Solution
Step 1: Check method name and parameters
The correct method is File.WriteAllText with the first parameter as the file path and the second as the text content.
Step 2: Validate syntax correctness
File.WriteAllText("greeting.txt", "Hello World"); matches the correct method name and parameter order. Options A, B, and D have incorrect method names or parameter order.
Final Answer:
File.WriteAllText("greeting.txt", "Hello World"); -> Option D
Quick Check:
Correct method and parameter order = File.WriteAllText("greeting.txt", "Hello World"); [OK]
Hint: Method name is File.WriteAllText(path, content) [OK]
Common Mistakes:
Swapping parameters order
Using incorrect method names like WriteText
Calling method on WriteAllText instead of File
3. What will be the content of the file "notes.txt" after running this code?
The first call writes "Line 1\nLine 2" to "notes.txt", creating or overwriting the file.
Step 2: Analyze second File.WriteAllText call
The second call overwrites the entire file content with "New Line", replacing previous text.
Final Answer:
New Line -> Option A
Quick Check:
Second WriteAllText overwrites file content [OK]
Hint: WriteAllText overwrites file, last call wins [OK]
Common Mistakes:
Assuming text appends instead of overwriting
Thinking both texts combine in file
Ignoring the second WriteAllText call
4. Identify the error in this code snippet that tries to write "Data" to "output.txt":
File.WriteAllText(output.txt, "Data");
medium
A. File.WriteAllText cannot write strings
B. Missing quotes around the file name output.txt
C. The method name should be WriteTextAll
D. The file path must be an absolute path
Solution
Step 1: Check the file path parameter
The file path must be a string, so it needs quotes around it. Here, output.txt is unquoted, causing a syntax error.
Step 2: Verify method name and parameter type
The method name is correct, and it accepts strings. Absolute path is not mandatory; relative paths work fine.
Final Answer:
Missing quotes around the file name output.txt -> Option B
Quick Check:
File path must be a string literal [OK]
Hint: File path must be in quotes as a string [OK]
Common Mistakes:
Forgetting quotes around file path
Mixing method name order
Thinking absolute path is required
5. You want to write multiple lines from a string array lines to a file named "log.txt" so that each element appears on its own line. Which code snippet correctly does this using File.WriteAllText?
hard
A. File.WriteAllText("log.txt", lines);
B. File.WriteAllText("log.txt", lines.ToString());
C. File.WriteAllText("log.txt", string.Join("\n", lines));
D. File.WriteAllText("log.txt", string.Concat(lines));
Solution
Step 1: Understand the input and desired output
We have a string array lines and want each element on its own line in the file.
Step 2: Check how to convert array to single string with line breaks
Using string.Join("\n", lines) joins array elements with newline characters, creating the correct multiline string.
Step 3: Validate other options
lines.ToString() returns type name, not content; passing array directly is invalid; string.Concat joins without separators.
Final Answer:
File.WriteAllText("log.txt", string.Join("\n", lines)); -> Option C
Quick Check:
Join array with \n for multiline text [OK]
Hint: Join array with \n before writing text file [OK]