DirectoryInfo Class
Represents a directory on the system.
Namespace: System.IO
Assembly: System.IO.FileSystem.dll
Assembly: System.IO.FileSystem.dll
Syntax
public sealed class DirectoryInfo : FileSystemInfo
Remarks
The DirectoryInfo class provides instance methods for creating, moving, and enumerating through directories and subdirectories. Instances of DirectoryInfo are used to inspect the contents of a directory.
To create a new DirectoryInfo object, you use one of its constructors:
public DirectoryInfo(string path);The path parameter specifies the full or relative path to the directory.
Constructors
DirectoryInfo Constructor
public DirectoryInfo(string path);Initializes a new instance of the DirectoryInfo class for the specified path.
path- A string representing the path to the directory. This parameter cannot be an empty string or contain only white space characters.
Properties
| Name | Description |
|---|---|
CreationTime |
Gets the creation date and time of the directory. |
CreationTimeUtc |
Gets the creation date and time, expressed in Coordinated Universal Time (UTC). |
Exists |
Gets a value indicating whether the directory or file represented by this FileSystemInfo object exists. |
Extension |
Gets the extension of the current file name. |
FullName |
Gets the full path of the directory. |
LastAccessTime |
Gets the date and time the current file or directory was last accessed. |
LastAccessTimeUtc |
Gets the date and time, expressed in Coordinated Universal Time (UTC), that the directory was last accessed. |
LastWriteTime |
Gets the date and time that the current file or directory was last written to. |
LastWriteTimeUtc |
Gets the date and time, expressed in Coordinated Universal Time (UTC), that the directory was last written to. |
Name |
Gets the name of the directory. |
Parent |
Gets the parent directory of the current directory. |
Root |
Gets the root directory of the path. |
Methods
| Name | Description |
|---|---|
Create() |
Creates the directory represented by this DirectoryInfo object. |
CreateSubdirectory(string path) |
Creates a subdirectory in the specified path. |
Delete() |
Deletes this directory and, optionally, all its contents. |
EnumerateDirectories() |
Returns an enumerable collection of directory names in the current directory. |
EnumerateFiles() |
Returns an enumerable collection of file names in the current directory. |
GetDirectories() |
Gets an array of DirectoryInfo objects representing the subdirectories in the current directory. |
GetFiles() |
Gets an array of FileInfo objects representing the files in the current directory. |
MoveTo(string destDirName) |
Moves this directory to a different path. |
Refresh() |
Resets the state of the object to its last state. |
ToString() |
Returns a string that represents the current object. |
Example
The following example demonstrates how to use the DirectoryInfo class to create a directory, get information about it, and list its contents.
using System;
using System.IO;
public class Example
{
public static void Main()
{
// Specify the directory path.
string myDirectoryPath = @"C:\MyTestDirectory";
try
{
// Create a DirectoryInfo object.
DirectoryInfo di = new DirectoryInfo(myDirectoryPath);
// Check if the directory exists, and if not, create it.
if (!di.Exists)
{
di.Create();
Console.WriteLine($"Directory '{myDirectoryPath}' created successfully.");
}
// Display information about the directory.
Console.WriteLine($"Directory Name: {di.Name}");
Console.WriteLine($"Full Path: {di.FullName}");
Console.WriteLine($"Creation Time: {di.CreationTime}");
Console.WriteLine($"Last Access Time: {di.LastAccessTime}");
Console.WriteLine($"Last Write Time: {di.LastWriteTime}");
// Create some dummy files for demonstration
File.WriteAllText(Path.Combine(myDirectoryPath, "file1.txt"), "Content of file 1");
File.WriteAllText(Path.Combine(myDirectoryPath, "file2.log"), "Log entry");
// Get and display the subdirectories.
Console.WriteLine("\nSubdirectories:");
DirectoryInfo[] subDirs = di.GetDirectories();
if (subDirs.Length == 0)
{
Console.WriteLine(" No subdirectories found.");
}
else
{
foreach (DirectoryInfo subDir in subDirs)
{
Console.WriteLine($" - {subDir.Name}");
}
}
// Get and display the files.
Console.WriteLine("\nFiles:");
FileInfo[] files = di.GetFiles();
if (files.Length == 0)
{
Console.WriteLine(" No files found.");
}
else
{
foreach (FileInfo file in files)
{
Console.WriteLine($" - {file.Name} ({file.Length} bytes)");
}
}
// Clean up: Delete the directory.
// di.Delete(true); // Uncomment to delete the directory after execution
// Console.WriteLine($"\nDirectory '{myDirectoryPath}' deleted successfully.");
}
catch (Exception e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
}
}
Tip: Use
EnumerateDirectories() and EnumerateFiles() for better performance when dealing with a large number of directories or files, as they return items one by one instead of loading them all into memory at once.