Bird
Raised Fist0
C Sharp (C#)programming~20 mins

File paths and Directory operations in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
File Paths and Directories Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this directory creation code?
Consider the following C# code that creates a directory and checks if it exists. What will be printed?
C Sharp (C#)
using System;
using System.IO;

class Program {
    static void Main() {
        string path = "TestDir";
        Directory.CreateDirectory(path);
        Console.WriteLine(Directory.Exists(path));
    }
}
ATrue
BFalse
CThrows IOException
DThrows UnauthorizedAccessException
Attempts:
2 left
💡 Hint
Think about what Directory.CreateDirectory does if the directory already exists.
Predict Output
intermediate
2:00remaining
What does this code print about file paths?
Given this C# code snippet, what will be the output?
C Sharp (C#)
using System;
using System.IO;

class Program {
    static void Main() {
        string filePath = "C:\\Users\\Public\\Documents\\report.txt";
        Console.WriteLine(Path.GetFileName(filePath));
    }
}
Areport.txt
BC:\Users\Public\Documents\report.txt
Creport
DDocuments
Attempts:
2 left
💡 Hint
Path.GetFileName returns the file name and extension from a full path.
Predict Output
advanced
2:00remaining
What is the output of this code listing files?
This code lists files in a directory. What will it print?
C Sharp (C#)
using System;
using System.IO;

class Program {
    static void Main() {
        string dir = "TestDir";
        Directory.CreateDirectory(dir);
        File.WriteAllText(Path.Combine(dir, "file1.txt"), "Hello");
        File.WriteAllText(Path.Combine(dir, "file2.txt"), "World");
        var files = Directory.GetFiles(dir);
        Console.WriteLine(files.Length);
    }
}
A1
B2
CThrows DirectoryNotFoundException
D0
Attempts:
2 left
💡 Hint
Files are created before listing, so count should reflect that.
Predict Output
advanced
2:00remaining
What error does this code raise when deleting a non-empty directory?
What happens when you run this code that tries to delete a directory containing files?
C Sharp (C#)
using System;
using System.IO;

class Program {
    static void Main() {
        string dir = "TestDir";
        Directory.CreateDirectory(dir);
        File.WriteAllText(Path.Combine(dir, "file.txt"), "data");
        Directory.Delete(dir);
        Console.WriteLine("Deleted");
    }
}
AThrows UnauthorizedAccessException
BPrints 'Deleted'
CThrows IOException
DThrows DirectoryNotFoundException
Attempts:
2 left
💡 Hint
Deleting a directory with files without recursive flag causes an error.
🧠 Conceptual
expert
3:00remaining
How many items are in the resulting dictionary after this path manipulation?
Consider this C# code that uses DirectoryInfo and FileInfo objects. How many entries will the dictionary contain?
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.IO;

class Program {
    static void Main() {
        string dir = "TestDir";
        Directory.CreateDirectory(dir);
        File.WriteAllText(Path.Combine(dir, "a.txt"), "A");
        File.WriteAllText(Path.Combine(dir, "b.txt"), "B");
        var dict = new Dictionary<string, long>();
        var directoryInfo = new DirectoryInfo(dir);
        foreach (var file in directoryInfo.GetFiles()) {
            dict[file.Name] = file.Length;
        }
        Console.WriteLine(dict.Count);
    }
}
AThrows IOException
B0
C1
D2
Attempts:
2 left
💡 Hint
Each file in the directory is added as a dictionary entry.

Practice

(1/5)
1. Which C# class is used to check if a directory exists on your computer?
easy
A. Path
B. File
C. Directory
D. StreamReader

Solution

  1. Step 1: Understand the purpose of Directory class

    The Directory class provides methods to work with folders, including checking if they exist.
  2. Step 2: Identify the correct method for existence check

    Directory.Exists(path) returns true if the folder exists, which is what we need.
  3. Final Answer:

    Directory -> Option C
  4. Quick Check:

    Directory = Folder check [OK]
Hint: Use Directory class to manage folders easily [OK]
Common Mistakes:
  • Confusing File class with Directory for folders
  • Using Path class to check existence
  • Trying to read folder like a file
2. Which of the following is the correct syntax to create a new directory named "Data" in C#?
easy
A. Directory.Create("Data");
B. File.CreateDirectory("Data");
C. Path.CreateDirectory("Data");
D. Directory.CreateDirectory("Data");

Solution

  1. Step 1: Identify the correct method to create directories

    The Directory class has a method called CreateDirectory to make new folders.
  2. Step 2: Check method names and classes

    Only Directory.CreateDirectory("Data") is valid syntax; others are incorrect or belong to wrong classes.
  3. Final Answer:

    Directory.CreateDirectory("Data"); -> Option D
  4. Quick Check:

    CreateDirectory method creates folders [OK]
Hint: Use Directory.CreateDirectory to make folders [OK]
Common Mistakes:
  • Using Directory.Create instead of CreateDirectory
  • Trying to create directory with File class
  • Using Path class for folder creation
3. What will be the output of this C# code?
string folder = "C:\\Users\\Public";
string fileName = "report.txt";
string fullPath = Path.Combine(folder, fileName);
Console.WriteLine(fullPath);
medium
A. C:/Users/Public/report.txt
B. C:\Users\Public\report.txt
C. C:\Users\Publicreport.txt
D. C:\Users\Public\

Solution

  1. Step 1: Understand Path.Combine behavior

    Path.Combine joins folder and file name with the correct directory separator for Windows (\).
  2. Step 2: Check the combined string output

    The result is "C:\Users\Public\report.txt" with backslashes and a single separator between folder and file.
  3. Final Answer:

    C:\Users\Public\report.txt -> Option B
  4. Quick Check:

    Path.Combine joins paths with \ [OK]
Hint: Path.Combine joins paths with correct separators [OK]
Common Mistakes:
  • Expecting forward slashes instead of backslashes
  • Missing separator between folder and file
  • Confusing output with folder path only
4. 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");
}
medium
A. Directory.Delete requires a second argument to delete non-empty folders
B. Directory.Exists should be File.Exists
C. The path string is incorrectly escaped
D. Console.WriteLine cannot be used inside if

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]
Hint: Use Directory.Delete(path, true) for non-empty folders [OK]
Common Mistakes:
  • Assuming Directory.Delete deletes non-empty folders by default
  • Using File.Exists to check folders
  • Incorrectly escaping path strings
5. You want to list all subdirectories inside "C:\\Projects" and print their full paths. Which code snippet correctly does this?
hard
A. foreach (var dir in Directory.GetDirectories("C:\\Projects")) { Console.WriteLine(dir); }
B. foreach (var file in Directory.GetFiles("C:\\Projects")) { Console.WriteLine(file); }
C. foreach (var dir in Path.GetDirectories("C:\\Projects")) { Console.WriteLine(dir); }
D. foreach (var dir in Directory.ListDirectories("C:\\Projects")) { Console.WriteLine(dir); }

Solution

  1. Step 1: Identify method to get subdirectories

    Directory.GetDirectories(path) returns an array of folder paths inside the given directory.
  2. Step 2: Use foreach to print each directory path

    Looping over the array and printing each path is done with foreach and Console.WriteLine.
  3. Final Answer:

    foreach (var dir in Directory.GetDirectories("C:\\Projects")) { Console.WriteLine(dir); } -> Option A
  4. Quick Check:

    GetDirectories lists folders [OK]
Hint: Use Directory.GetDirectories to list folders [OK]
Common Mistakes:
  • Using GetFiles instead of GetDirectories
  • Trying to use Path class for directory listing
  • Using non-existent Directory.ListDirectories method