C# `DirectoryInfo` Class: Object-Oriented Directory Manipulation
Master directory manipulation in C# using the `DirectoryInfo` class. This tutorial demonstrates how to create, delete, and manage directories, including robust error handling. Learn to use `DirectoryInfo` for efficient and object-oriented file system operations.
Working with Directories in C# using the `DirectoryInfo` Class
Introduction to `DirectoryInfo`
The C# `DirectoryInfo` class (part of the `System.IO` namespace) provides a way to work with directories (folders) in a more object-oriented manner. It offers properties and methods for creating, deleting, moving, and getting information about directories and their contents. Unlike the static `Directory` class, `DirectoryInfo` works with directory objects, making it suitable for more complex directory manipulation.
`DirectoryInfo` Class Members
The `DirectoryInfo` class includes constructors, properties, and methods for managing directories.
Constructors
Constructor | Description |
---|---|
DirectoryInfo(string path) |
Creates a new `DirectoryInfo` object for the specified path. |
Properties
Property | Description |
---|---|
Attributes |
Gets or sets the attributes of the directory (e.g., read-only, hidden). |
CreationTime |
Gets or sets the directory's creation time. |
CreationTimeUtc |
Gets or sets the creation time in UTC (Coordinated Universal Time). |
Exists |
Indicates whether the directory exists. |
Extension |
Gets the extension of the directory (always empty for directories). |
FullName |
Gets the full path of the directory. |
LastAccessTime |
Gets or sets the last access time. |
LastAccessTimeUtc |
Gets or sets the last access time in UTC. |
LastWriteTime |
Gets or sets the last write time. |
LastWriteTimeUtc |
Gets or sets the last write time in UTC. |
Name |
Gets the name of the directory. |
Parent |
Gets the parent directory. |
Root |
Gets the root directory of the path. |
Methods
Method | Description |
---|---|
Create() |
Creates the directory. |
CreateSubdirectory() |
Creates a subdirectory. |
Delete() |
Deletes the directory (must be empty). |
EnumerateDirectories() |
Gets an enumerable collection of subdirectories. |
EnumerateFiles() |
Gets an enumerable collection of files. |
GetAccessControl() |
Gets the directory's access control list (ACL). |
GetDirectories() |
Gets an array of subdirectories. |
GetFiles() |
Gets an array of files. |
GetType() |
Gets the type of the object. |
MoveTo() |
Moves the directory to a new location. |
Refresh() |
Refreshes the directory information. |
SetAccessControl() |
Sets the directory's access control list. |
ToString() |
Returns a string representation of the path. |
Example: Creating and Deleting a Directory
This example demonstrates creating and then deleting a directory using the `DirectoryInfo` class. It includes error handling using a `try-catch` block to manage potential exceptions. Remember to replace `"F:\tutorialsarena"` with an actual path on your system.
C# Code
using System;
using System.IO;
public class DirectoryInfoExample {
public static void Main(string[] args) {
DirectoryInfo dir = new DirectoryInfo(@"F:\tutorialsarena");
try {
if (dir.Exists) {
Console.WriteLine("Directory already exists.");
} else {
dir.Create();
Console.WriteLine("Directory created successfully.");
dir.Delete();
Console.WriteLine("Directory deleted successfully.");
}
} catch (Exception ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Conclusion
The `DirectoryInfo` class provides a powerful and object-oriented way to interact with directories in C#. It's important to understand its properties and methods and always handle potential exceptions appropriately.