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
File paths and Directory operations
📖 Scenario: You are organizing your computer files for a small project. You want to create folders and check file paths to keep everything neat and easy to find.
🎯 Goal: Build a simple C# program that creates a directory, checks if a file path exists, and lists files in a directory.
📋 What You'll Learn
Create a string variable with a directory path
Create a string variable with a file path
Use Directory.CreateDirectory to create the directory
Use File.Exists to check if the file exists
Use Directory.GetFiles to list files in the directory
Print the results to the console
💡 Why This Matters
🌍 Real World
Managing files and folders is a common task when organizing data, saving reports, or handling user uploads in software.
💼 Career
Understanding file paths and directory operations is essential for software developers, system administrators, and anyone working with file management in applications.
Progress0 / 4 steps
1
Create directory and file path variables
Create a string variable called folderPath with the value "C:\\ExampleFolder" and a string variable called filePath with the value "C:\\ExampleFolder\\example.txt".
C Sharp (C#)
Hint
Use double backslashes \\ in the string to represent a single backslash in file paths.
2
Create the directory
Use Directory.CreateDirectory(folderPath) to create the directory at the path stored in folderPath.
C Sharp (C#)
Hint
Use the Directory.CreateDirectory method and pass folderPath as the argument.
3
Check if the file exists and list files
Create a boolean variable called fileExists and set it to File.Exists(filePath). Then create a string array called files and set it to Directory.GetFiles(folderPath).
C Sharp (C#)
Hint
Use File.Exists to check the file and Directory.GetFiles to get all files in the folder.
4
Print the results
Print the value of fileExists using Console.WriteLine. Then use a foreach loop with variable file to print each file path in the files array.
C Sharp (C#)
Hint
Use Console.WriteLine(fileExists) and a foreach loop to print each file path.
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
Step 1: Understand the purpose of Directory class
The Directory class provides methods to work with folders, including checking if they exist.
Step 2: Identify the correct method for existence check
Directory.Exists(path) returns true if the folder exists, which is what we need.
Final Answer:
Directory -> Option C
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
Step 1: Identify the correct method to create directories
The Directory class has a method called CreateDirectory to make new folders.
Step 2: Check method names and classes
Only Directory.CreateDirectory("Data") is valid syntax; others are incorrect or belong to wrong classes.
Final Answer:
Directory.CreateDirectory("Data"); -> Option D
Quick Check:
CreateDirectory method creates folders [OK]
Hint: Use Directory.CreateDirectory to make folders [OK]