DirectoryInfo in C# is a class in the System.IO namespace that provides properties and methods to work with directories and subdirectories.
It allows you to create, delete, move, and access directory information, as well as retrieve files and subdirectories within a directory.
Unlike the Directory class, which is static, DirectoryInfo is instance-based, meaning you need to create an object to represent a specific directory.
In this tutorial, we’ll cover:
1. Basics of DirectoryInfo
DirectoryInfo is an instance-based class that represents a specific directory. To use DirectoryInfo, you create an instance with a path to the directory, and then you can use its properties and methods to work with that directory.
Example: Basic DirectoryInfo Setup
using System; using System.IO; public class DirectoryInfoExample { public static void Main() { DirectoryInfo dirInfo = new DirectoryInfo("TestDirectory"); // Use dirInfo to work with the directory } }
Here, dirInfo represents the directory named “TestDirectory.” You can use this instance to access information or perform operations on this directory.
2. Retrieving Directory Information
DirectoryInfo provides several properties to get information about a directory, such as its name, path, creation date, and parent directory.
Example: Getting Directory Information
using System; using System.IO; public class DirectoryInfoGetExample { public static void Main() { DirectoryInfo dirInfo = new DirectoryInfo("TestDirectory"); if (dirInfo.Exists) { Console.WriteLine("Directory Name: " + dirInfo.Name); Console.WriteLine("Full Path: " + dirInfo.FullName); Console.WriteLine("Parent Directory: " + dirInfo.Parent); Console.WriteLine("Creation Time: " + dirInfo.CreationTime); Console.WriteLine("Last Access Time: " + dirInfo.LastAccessTime); Console.WriteLine("Last Write Time: " + dirInfo.LastWriteTime); } else { Console.WriteLine("Directory does not exist."); } } }
Explanation:
- Name, FullName, Parent, CreationTime, LastAccessTime, and LastWriteTime are properties of DirectoryInfo that provide details about the directory.
Output (example):
Directory Name: TestDirectory Full Path: C:\path\to\TestDirectory Parent Directory: C:\path\to Creation Time: 10/30/2024 10:00:00 AM Last Access Time: 10/30/2024 10:05:00 AM Last Write Time: 10/30/2024 10:10:00 AM
3. Creating and Deleting Directories with DirectoryInfo
You can use DirectoryInfo to create and delete directories.
Example: Creating a Directory
using System; using System.IO; public class DirectoryInfoCreateExample { public static void Main() { DirectoryInfo dirInfo = new DirectoryInfo("NewDirectory"); if (!dirInfo.Exists) { dirInfo.Create(); Console.WriteLine("Directory created: " + dirInfo.FullName); } else { Console.WriteLine("Directory already exists."); } } }
Explanation:
- Create() creates the directory if it doesn’t already exist.
Example: Deleting a Directory
using System; using System.IO; public class DirectoryInfoDeleteExample { public static void Main() { DirectoryInfo dirInfo = new DirectoryInfo("NewDirectory"); if (dirInfo.Exists) { dirInfo.Delete(); Console.WriteLine("Directory deleted: " + dirInfo.FullName); } else { Console.WriteLine("Directory does not exist."); } } }
Explanation:
- Delete() deletes the directory. Note that if the directory contains files or subdirectories, you’ll need to specify true to delete it recursively (dirInfo.Delete(true)).
4. Moving and Renaming Directories
DirectoryInfo.MoveTo() allows you to move a directory to a new location or rename it by specifying a new path.
Example: Moving and Renaming a Directory
using System; using System.IO; public class DirectoryInfoMoveExample { public static void Main() { DirectoryInfo dirInfo = new DirectoryInfo("NewDirectory"); if (dirInfo.Exists) { string newLocation = "RenamedDirectory"; dirInfo.MoveTo(newLocation); Console.WriteLine("Directory moved and renamed to: " + newLocation); } else { Console.WriteLine("Directory does not exist."); } } }
Explanation:
- MoveTo(newLocation) moves the directory to the specified path. This can also be used to rename the directory.
5. Working with Files in a Directory
DirectoryInfo provides methods to access files and subdirectories within a directory, such as GetFiles() and GetDirectories().
Example: Listing Files in a Directory
using System; using System.IO; public class DirectoryInfoListFilesExample { public static void Main() { DirectoryInfo dirInfo = new DirectoryInfo("SampleDirectory"); if (dirInfo.Exists) { FileInfo[] files = dirInfo.GetFiles(); Console.WriteLine("Files in " + dirInfo.Name + ":"); foreach (FileInfo file in files) { Console.WriteLine(" - " + file.Name + " (" + file.Length + " bytes)"); } } else { Console.WriteLine("Directory does not exist."); } } }
Explanation:
- GetFiles() retrieves an array of FileInfo objects representing each file in the directory.
Output (example):
Files in SampleDirectory: - file1.txt (1024 bytes) - file2.doc (2048 bytes) - file3.jpg (5120 bytes)
6. Practical Example: Listing All Files and Subdirectories
In this example, we’ll recursively list all files and subdirectories within a directory.
Example: Recursive Listing of Files and Subdirectories
using System; using System.IO; public class DirectoryInfoRecursiveExample { public static void ListDirectoryContents(DirectoryInfo dirInfo, string indent = "") { // List all files in the directory FileInfo[] files = dirInfo.GetFiles(); foreach (FileInfo file in files) { Console.WriteLine(indent + "File: " + file.Name + " (" + file.Length + " bytes)"); } // List all subdirectories DirectoryInfo[] subDirs = dirInfo.GetDirectories(); foreach (DirectoryInfo subDir in subDirs) { Console.WriteLine(indent + "Directory: " + subDir.Name); ListDirectoryContents(subDir, indent + " "); // Recursive call for subdirectories } } public static void Main() { DirectoryInfo dirInfo = new DirectoryInfo("SampleDirectory"); if (dirInfo.Exists) { Console.WriteLine("Listing contents of " + dirInfo.FullName + ":"); ListDirectoryContents(dirInfo); } else { Console.WriteLine("Directory does not exist."); } } }
Explanation:
- ListDirectoryContents() recursively lists files and subdirectories, indenting each level for readability.
- GetFiles() retrieves files, and GetDirectories() retrieves subdirectories within the specified directory.
Sample Output:
Listing contents of C:\SampleDirectory: File: file1.txt (1024 bytes) File: file2.doc (2048 bytes) Directory: SubDir1 File: file3.jpg (5120 bytes) Directory: NestedSubDir File: file4.png (1024 bytes) Directory: SubDir2 File: file5.mp3 (8192 bytes)
Summary
DirectoryInfo in C# provides a flexible and powerful way to work with directories, allowing you to retrieve directory information, perform operations, and manage files and subdirectories.
Here’s a recap:
- Basic Setup: Create a DirectoryInfo object for a specific directory.
- Retrieving Information: Properties like Name, FullName, Parent, CreationTime, LastAccessTime, and LastWriteTime provide details about the directory.
- Creating and Deleting:
- Create(): Creates a new directory.
- Delete(): Deletes the directory, with an option for recursive deletion.
- Moving and Renaming: MoveTo() can move or rename the directory.
- Working with Files and Subdirectories:
- GetFiles(): Retrieves an array of files in the directory.
- GetDirectories(): Retrieves an array of subdirectories.
- Recursive Listing: Use recursive functions to list files and directories at all levels within a directory.
DirectoryInfo is an essential class for applications that need to manage file systems, providing easy access and control over directories and their contents.