-
Notifications
You must be signed in to change notification settings - Fork 0
DirectoryInformation Class
Provides read-only information about a directory, including its size, file count, and subdirectory count. This class represents metadata for a specific directory on the file system.
Namespace: Rheo.Storage.Information
Assembly: Rheo.Storage.dll
public sealed class DirectoryInformation : StorageInformation, IEquatable<DirectoryInformation>Object → StorageInformation → DirectoryInformation
- IEquatable<DirectoryInformation>
| Constructor | Description |
|---|---|
| DirectoryInformation(String) | Initializes a new instance of the DirectoryInformation class for the specified absolute directory path. |
| Property | Type | Description |
|---|---|---|
| NoOfFiles | Int32 | Gets the total number of files within the directory and its subdirectories. |
| NoOfDirectories | Int32 | Gets the total number of directories within the current directory and its subdirectories. |
| Size | Int64 | Gets the total size of all files in the directory tree in bytes. |
| Method | Description |
|---|---|
| Equals(DirectoryInformation) | Determines whether the specified DirectoryInformation is equal to the current instance. |
| Equals(Object) | Determines whether the specified object is equal to the current instance. |
| GetHashCode() | Returns the hash code for this instance. |
| ToString() | Returns a string representation of the directory information. |
| Operator | Description |
|---|---|
| Equality(DirectoryInformation, DirectoryInformation) | Determines whether two DirectoryInformation instances are equal. |
| Inequality(DirectoryInformation, DirectoryInformation) | Determines whether two DirectoryInformation instances are not equal. |
DirectoryInformation is immutable and retrieves information recursively for the specified directory and all its subdirectories. If the application lacks sufficient permissions to access parts of the directory tree, some properties may return fallback values (such as -1 for counts or 0 for size). This class is thread-safe for concurrent read operations.
Initializes a new instance of the DirectoryInformation class for the specified absolute directory path.
public DirectoryInformation(string absolutePath)absolutePath String
The absolute path of the directory to retrieve information for. The path must refer to an existing directory.
Thrown if the directory specified by absolutePath does not exist.
Gets the total number of files within the directory and its subdirectories.
public int NoOfFiles { get; }The total file count, or -1 if access is denied.
This property attempts to retrieve the file count recursively. If the application does not have the necessary permissions to access the directory or its subdirectories, the property returns -1 to indicate that the operation could not be completed.
Gets the total number of directories within the current directory and its subdirectories.
public int NoOfDirectories { get; }The total directory count, or -1 if access is denied.
This property attempts to retrieve the count of all directories recursively. If a security exception occurs (e.g., insufficient permissions to access the directory), the property returns -1.
Gets the total size of all files in the directory tree in bytes.
public override long Size { get; }The total size in bytes of all files in the directory and its subdirectories.
The size is calculated recursively by summing the sizes of all files in the directory tree. If access to any subdirectory is denied, that portion is skipped and the size calculation continues with accessible directories.
Determines whether the specified DirectoryInformation is equal to the current instance.
public bool Equals(DirectoryInformation? other)other DirectoryInformation
The DirectoryInformation to compare with the current instance.
true if the instances are equal; otherwise, false.
Equality is based on path, file count, and directory count.
Determines whether the specified object is equal to the current instance.
public override bool Equals(object? obj)obj Object
The object to compare with the current instance.
true if the object is a DirectoryInformation with equal values; otherwise, false.
Returns the hash code for this instance.
public override int GetHashCode()A hash code based on the directory path, file count, and directory count.
Returns a string representation of the directory information.
public override string ToString()A string in the format: DisplayName (Files=X, Directories=Y, Size=Z)
Determines whether two DirectoryInformation instances are equal.
public static bool operator ==(DirectoryInformation? left, DirectoryInformation? right)left DirectoryInformation
The first instance to compare.
right DirectoryInformation
The second instance to compare.
true if the instances are equal; otherwise, false.
Determines whether two DirectoryInformation instances are not equal.
public static bool operator !=(DirectoryInformation? left, DirectoryInformation? right)left DirectoryInformation
The first instance to compare.
right DirectoryInformation
The second instance to compare.
true if the instances are not equal; otherwise, false.
The following example demonstrates retrieving directory information:
using Rheo.Storage.Information;
var dirInfo = new DirectoryInformation(@"C:\Projects\MyApp");
Console.WriteLine($"Directory: {dirInfo.DisplayName}");
Console.WriteLine($"Total Files: {dirInfo.NoOfFiles}");
Console.WriteLine($"Total Directories: {dirInfo.NoOfDirectories}");
Console.WriteLine($"Total Size: {dirInfo.FormattedSize}");
Console.WriteLine($"Created: {dirInfo.CreationTime}");
Console.WriteLine($"Modified: {dirInfo.LastWriteTime}");The following example shows how to handle permission issues:
using Rheo.Storage.Information;
var dirInfo = new DirectoryInformation(@"C:\System\Protected");
if (dirInfo.NoOfFiles == -1 || dirInfo.NoOfDirectories == -1)
{
Console.WriteLine("Warning: Access denied to some or all of the directory.");
Console.WriteLine("File and directory counts may be incomplete.");
}
else
{
Console.WriteLine($"Files: {dirInfo.NoOfFiles}");
Console.WriteLine($"Directories: {dirInfo.NoOfDirectories}");
}The following example demonstrates directory comparison:
using Rheo.Storage.Information;
var dir1 = new DirectoryInformation(@"C:\Projects\Version1");
var dir2 = new DirectoryInformation(@"C:\Projects\Version2");
Console.WriteLine("Comparison:");
Console.WriteLine($" Version1: {dir1.NoOfFiles} files, {dir1.FormattedSize}");
Console.WriteLine($" Version2: {dir2.NoOfFiles} files, {dir2.FormattedSize}");
if (dir1 == dir2)
{
Console.WriteLine("Directories have the same structure and count.");
}
else
{
var fileDiff = dir2.NoOfFiles - dir1.NoOfFiles;
var sizeDiff = dir2.Size - dir1.Size;
Console.WriteLine($"File difference: {fileDiff:+#;-#;0}");
Console.WriteLine($"Size difference: {sizeDiff:+#;-#;0} bytes");
}The following example shows how to find large directories:
using Rheo.Storage.Information;
string[] directories = Directory.GetDirectories(@"C:\Projects", "*", SearchOption.TopDirectoryOnly);
var largeDirectories = new List<DirectoryInformation>();
const long threshold = 100 * 1024 * 1024; // 100 MB
foreach (var dir in directories)
{
try
{
var dirInfo = new DirectoryInformation(dir);
if (dirInfo.Size > threshold)
{
largeDirectories.Add(dirInfo);
}
}
catch (DirectoryNotFoundException)
{
// Skip directories that no longer exist
}
}
// Sort by size descending
largeDirectories = largeDirectories.OrderByDescending(d => d.Size).ToList();
Console.WriteLine($"Directories larger than {threshold / (1024 * 1024)} MB:");
foreach (var dir in largeDirectories)
{
Console.WriteLine($" {dir.DisplayName}: {dir.FormattedSize}");
}The following example demonstrates analyzing a directory tree:
using Rheo.Storage.Information;
void AnalyzeDirectory(string path, int indent = 0)
{
var dirInfo = new DirectoryInformation(path);
var prefix = new string(' ', indent * 2);
Console.WriteLine($"{prefix}{dirInfo.DisplayName}");
Console.WriteLine($"{prefix} Files: {dirInfo.NoOfFiles}");
Console.WriteLine($"{prefix} Subdirs: {dirInfo.NoOfDirectories}");
Console.WriteLine($"{prefix} Size: {dirInfo.FormattedSize}");
}
AnalyzeDirectory(@"C:\Projects\MyApp");- Directory size calculation is performed recursively and may be slow for large directory trees
- File and directory counts require full enumeration of the directory tree
- The constructor calculates size immediately, which may block for large directories
- Consider caching DirectoryInformation instances for frequently accessed directories
This class is thread-safe for read operations once constructed. However, construction itself performs I/O operations that block until complete.
-
Storage Objects
-
Storage Information
-
Platform-Specific
-
Content-Type Analysis
-
Results
-
Models
-
-
Events & Progress