File paths and directory operations help you find, create, and manage folders and files on your computer.
File paths and Directory operations in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
using System.IO; // Get full path string fullPath = Path.GetFullPath(relativePath); // Check if directory exists bool exists = Directory.Exists(path); // Create directory Directory.CreateDirectory(path); // List files in directory string[] files = Directory.GetFiles(path); // Delete directory Directory.Delete(path, recursive: true);
Use Path class to work with file and folder paths safely.
Use Directory class to create, check, list, and delete folders.
C:\Users\Public exists and prints true or false.string path = "C:\\Users\\Public";
bool exists = Directory.Exists(path);
Console.WriteLine(exists);MyFolder inside C:\Temp.string newFolder = "C:\\Temp\\MyFolder"; Directory.CreateDirectory(newFolder); Console.WriteLine("Folder created");
C:\Temp folder.string[] files = Directory.GetFiles("C:\\Temp");
foreach (string file in files)
{
Console.WriteLine(file);
}MyFolder and all its contents.Directory.Delete("C:\\Temp\\MyFolder", recursive: true); Console.WriteLine("Folder deleted");
This program checks if a folder named TestFolder exists. If not, it creates it. Then it creates a text file inside that folder and writes a message. Finally, it lists all files in the folder.
using System; using System.IO; class Program { static void Main() { string folderPath = "TestFolder"; // Check if folder exists if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); Console.WriteLine("Folder created: " + folderPath); } else { Console.WriteLine("Folder already exists: " + folderPath); } // Create a file path inside the folder string filePath = Path.Combine(folderPath, "example.txt"); // Write text to the file File.WriteAllText(filePath, "Hello, file system!"); Console.WriteLine("File created with text: " + filePath); // List files in the folder string[] files = Directory.GetFiles(folderPath); Console.WriteLine("Files in folder:"); foreach (string file in files) { Console.WriteLine(file); } } }
Use Path.Combine to join folder and file names safely without errors.
Deleting a folder with recursive: true removes all files and subfolders inside it.
Always check if a folder or file exists before trying to use it to avoid errors.
File paths tell your program where files and folders are on your computer.
Use Directory class to create, check, list, and delete folders.
Use Path class to work safely with file and folder paths.
Practice
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 CQuick Check:
Directory = Folder check [OK]
- Confusing File class with Directory for folders
- Using Path class to check existence
- Trying to read folder like a file
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 DQuick Check:
CreateDirectory method creates folders [OK]
- Using Directory.Create instead of CreateDirectory
- Trying to create directory with File class
- Using Path class for folder creation
string folder = "C:\\Users\\Public"; string fileName = "report.txt"; string fullPath = Path.Combine(folder, fileName); Console.WriteLine(fullPath);
Solution
Step 1: Understand Path.Combine behavior
Path.Combine joins folder and file name with the correct directory separator for Windows (\).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.Final Answer:
C:\Users\Public\report.txt -> Option BQuick Check:
Path.Combine joins paths with \ [OK]
- Expecting forward slashes instead of backslashes
- Missing separator between folder and file
- Confusing output with folder path only
string path = "C:\\Temp";
if (Directory.Exists(path))
{
Directory.Delete(path);
Console.WriteLine("Deleted");
}Solution
Step 1: Understand Directory.Delete behavior
Directory.Delete(path) without a second argument only deletes empty folders.Step 2: Check if folder might be non-empty
If folder has files, Directory.Delete(path, true) is needed to delete recursively.Final Answer:
Directory.Delete requires a second argument to delete non-empty folders -> Option AQuick Check:
Delete non-empty folder needs recursive flag [OK]
- Assuming Directory.Delete deletes non-empty folders by default
- Using File.Exists to check folders
- Incorrectly escaping path strings
Solution
Step 1: Identify method to get subdirectories
Directory.GetDirectories(path) returns an array of folder paths inside the given directory.Step 2: Use foreach to print each directory path
Looping over the array and printing each path is done with foreach and Console.WriteLine.Final Answer:
foreach (var dir in Directory.GetDirectories("C:\\Projects")) { Console.WriteLine(dir); } -> Option AQuick Check:
GetDirectories lists folders [OK]
- Using GetFiles instead of GetDirectories
- Trying to use Path class for directory listing
- Using non-existent Directory.ListDirectories method
