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

Writing text files in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
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
Which namespace is required to use File.WriteAllText?
ASystem.IO
BSystem.Net
CSystem.Text
DSystem.Threading
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
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
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.