Bird
Raised Fist0

You want to append a new line "Log entry" to an existing file "log.txt" without overwriting it. Which C# code snippet correctly does this?

hard🚀 Application Q8 of Q15
C Sharp (C#) - File IO
You want to append a new line "Log entry" to an existing file "log.txt" without overwriting it. Which C# code snippet correctly does this?
AFile.AppendAllText("log.txt", "Log entry\n");
BFile.WriteAllText("log.txt", "Log entry");
Cnew StreamWriter("log.txt").WriteLine("Log entry");
DFile.ReadAllText("log.txt").WriteLine("Log entry");
Step-by-Step Solution
Solution:
  1. Step 1: Understand append vs overwrite

    File.AppendAllText adds text to the end without erasing existing content.
  2. Step 2: Validate options

    File.AppendAllText("log.txt", "Log entry\n"); uses AppendAllText correctly; others overwrite or misuse methods.
  3. Final Answer:

    File.AppendAllText("log.txt", "Log entry\n"); -> Option A
  4. Quick Check:

    Appending text = File.AppendAllText [OK]
Quick Trick: Use File.AppendAllText to add without overwriting [OK]
Common Mistakes:
MISTAKES
  • Using WriteAllText which overwrites file
  • Not disposing StreamWriter properly
  • Trying to write on string returned by ReadAllText

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes