File paths and directory operations help you find, create, and manage folders and files on your computer.
0
0
File paths and Directory operations in C Sharp (C#)
Introduction
When you want to save a file in a specific folder.
When you need to check if a folder or file exists before using it.
When you want to create a new folder to organize files.
When you want to list all files inside a folder.
When you want to delete or move files and folders.
Syntax
C Sharp (C#)
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.
Examples
This checks if the folder
C:\Users\Public exists and prints true or false.C Sharp (C#)
string path = "C:\\Users\\Public";
bool exists = Directory.Exists(path);
Console.WriteLine(exists);This creates a new folder called
MyFolder inside C:\Temp.C Sharp (C#)
string newFolder = "C:\\Temp\\MyFolder"; Directory.CreateDirectory(newFolder); Console.WriteLine("Folder created");
This lists all files inside the
C:\Temp folder.C Sharp (C#)
string[] files = Directory.GetFiles("C:\\Temp");
foreach (string file in files)
{
Console.WriteLine(file);
}This deletes the folder
MyFolder and all its contents.C Sharp (C#)
Directory.Delete("C:\\Temp\\MyFolder", recursive: true); Console.WriteLine("Folder deleted");
Sample Program
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.
C Sharp (C#)
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); } } }
OutputSuccess
Important Notes
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.
Summary
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.