Bird
0
0

Identify the error in this code snippet that tries to delete a directory:

medium📝 Debug Q14 of 15
C Sharp (C#) - File IO
Identify the error in this code snippet that tries to delete a directory:
string path = "C:\\Temp";
if (Directory.Exists(path))
{
    Directory.Delete(path);
    Console.WriteLine("Deleted");
}
ADirectory.Delete requires a second argument to delete non-empty folders
BDirectory.Exists should be File.Exists
CThe path string is incorrectly escaped
DConsole.WriteLine cannot be used inside if
Step-by-Step Solution
Solution:
  1. Step 1: Understand Directory.Delete behavior

    Directory.Delete(path) without a second argument only deletes empty folders.
  2. Step 2: Check if folder might be non-empty

    If folder has files, Directory.Delete(path, true) is needed to delete recursively.
  3. Final Answer:

    Directory.Delete requires a second argument to delete non-empty folders -> Option A
  4. Quick Check:

    Delete non-empty folder needs recursive flag [OK]
Quick Trick: Use Directory.Delete(path, true) for non-empty folders [OK]
Common Mistakes:
MISTAKES
  • Assuming Directory.Delete deletes non-empty folders by default
  • Using File.Exists to check folders
  • Incorrectly escaping path strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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