C# Files (I/O) Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To call Directory.GetFiles()
with multiple filters, you can use the overload of the method that takes a search pattern as a parameter. The search pattern can contain multiple filters separated by the pipe (|
) character. Here is an example that searches for files with a .txt
or .docx
extension in the specified directory:
string[] files = Directory.GetFiles(@"C:\MyFolder", "*.txt|*.docx");
To get the full path directory from a filename, you can use the Path.GetDirectoryName()
method. This method returns the directory information for the specified path without the file name.
string fullPath = @"C:\MyFolder\myfile.txt"; string directory = Path.GetDirectoryName(fullPath); // Returns "C:\MyFolder"
To get the folder name from a full path filename, you can use the Path.GetFileName()
method. This method returns the file name and extension of the specified path string.
string fullPath = @"C:\MyFolder\myfile.txt"; string fileName = Path.GetFileName(fullPath); // Returns "myfile.txt" string folderName = Path.GetFileName(Path.GetDirectoryName(fullPath)); // Returns "MyFolder"
To delete all files and folders in a directory, you can use the Directory.Delete()
method with the true
option to delete all files and folders recursively. Here is an example:
string directoryPath = @"C:\MyFolder"; Directory.Delete(directoryPath, true);
To delete a directory with read-only files, you must first remove the read-only attribute from the files before you can delete them. You can do this using the File.SetAttributes()
method with the FileAttributes.Normal
option to remove the read-only attribute. Here is an example:
string directoryPath = @"C:\MyFolder"; foreach (string file in Directory.GetFiles(directoryPath, "*", SearchOption.AllDirectories)) { File.SetAttributes(file, FileAttributes.Normal); } Directory.Delete(directoryPath, true);
To delete files older than 6 months old in a directory, you can use the Directory.GetFiles()
method to get all the files in the directory, and then use the File.GetLastWriteTime()
method to check the last write time of each file. You can then use the File.Delete()
method to delete the files that are older than 6 months. Here is an example:
string directoryPath = @"C:\MyFolder"; DateTime sixMonthsAgo = DateTime.Now.AddMonths(-6); foreach (string file in Directory.GetFiles(directoryPath)) { if (File.GetLastWriteTime(file) < sixMonthsAgo) { File.Delete(file); } }
"Working with directories in C#": A general introduction to handling directories in C#.
string directoryPath = "C:\\Path\\to\\directory"; // Check if directory exists if (Directory.Exists(directoryPath)) { // Directory operations here }
"Creating directories in C#": Creating a new directory in C#.
string newDirectoryPath = "C:\\Path\\to\\new-directory"; // Create a new directory Directory.CreateDirectory(newDirectoryPath);
"Checking if a directory exists in C#": Verifying if a directory exists in C#.
string directoryPath = "C:\\Path\\to\\directory"; // Check if directory exists if (Directory.Exists(directoryPath)) { // Directory exists }
"Listing files in a directory with C#": Retrieving a list of files in a directory in C#.
string directoryPath = "C:\\Path\\to\\files"; // Get list of files in the directory string[] files = Directory.GetFiles(directoryPath);
"Recursively traversing directories in C#": Recursively exploring directories in C#.
string rootDirectory = "C:\\Root\\Directory"; // Recursively traverse directories TraverseDirectories.RecursivelyTraverse(rootDirectory);
"Deleting directories in C#": Deleting a directory in C#.
string directoryPath = "C:\\Path\\to\\directory"; // Delete directory Directory.Delete(directoryPath);
"Moving and renaming directories in C#": Moving and renaming directories in C#.
string sourceDirectory = "C:\\Path\\to\\source-directory"; string destinationDirectory = "C:\\Path\\to\\destination-directory"; // Move directory Directory.Move(sourceDirectory, destinationDirectory);
"Copying directories in C#": Copying directories in C#.
string sourceDirectory = "C:\\Path\\to\\source-directory"; string destinationDirectory = "C:\\Path\\to\\destination-directory"; // Copy directory CopyDirectory.Copy(sourceDirectory, destinationDirectory);
"Getting directory information in C#": Retrieving information about a directory in C#.
string directoryPath = "C:\\Path\\to\\directory"; // Get directory information DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
"Handling directory not found errors in C#": Handling errors when the directory is not found in C#.
string directoryPath = "C:\\Nonexistent\\Directory"; try { // Operations on directory DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath); } catch (DirectoryNotFoundException) { Console.WriteLine("Directory not found!"); }
"Using Directory.CreateDirectory in C#":
Using Directory.CreateDirectory
to create a directory in C#.
string newDirectoryPath = "C:\\Path\\to\\new-directory"; // Create directory if it doesn't exist Directory.CreateDirectory(newDirectoryPath);
"Determining the root directory in C#": Finding the root directory in C#.
string fullPath = "C:\\Path\\to\\nested\\directory\\file.txt"; // Get root directory string rootDirectory = Path.GetPathRoot(fullPath);
"Retrieving subdirectories in C#": Fetching subdirectories of a directory in C#.
string directoryPath = "C:\\Path\\to\\parent-directory"; // Get subdirectories string[] subdirectories = Directory.GetDirectories(directoryPath);
"Accessing directory attributes in C#": Accessing and modifying directory attributes in C#.
string directoryPath = "C:\\Path\\to\\directory"; // Get directory attributes FileAttributes attributes = File.GetAttributes(directoryPath); // Modify directory attributes attributes |= FileAttributes.Hidden; File.SetAttributes(directoryPath, attributes);