Skip to content

DirectoryInformation Class

Naveen Dharmathunga edited this page Jan 19, 2026 · 1 revision

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

Definition

public sealed class DirectoryInformation : StorageInformation, IEquatable<DirectoryInformation>

Inheritance

ObjectStorageInformation → DirectoryInformation

Implements

Constructors

Constructor Description
DirectoryInformation(String) Initializes a new instance of the DirectoryInformation class for the specified absolute directory path.

Properties

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.

Methods

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.

Operators

Operator Description
Equality(DirectoryInformation, DirectoryInformation) Determines whether two DirectoryInformation instances are equal.
Inequality(DirectoryInformation, DirectoryInformation) Determines whether two DirectoryInformation instances are not equal.

Remarks

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.


Constructor Details

DirectoryInformation(String)

Initializes a new instance of the DirectoryInformation class for the specified absolute directory path.

public DirectoryInformation(string absolutePath)

Parameters

absolutePath String

The absolute path of the directory to retrieve information for. The path must refer to an existing directory.

Exceptions

DirectoryNotFoundException

Thrown if the directory specified by absolutePath does not exist.


Property Details

NoOfFiles

Gets the total number of files within the directory and its subdirectories.

public int NoOfFiles { get; }

Property Value

Int32

The total file count, or -1 if access is denied.

Remarks

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.


NoOfDirectories

Gets the total number of directories within the current directory and its subdirectories.

public int NoOfDirectories { get; }

Property Value

Int32

The total directory count, or -1 if access is denied.

Remarks

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.


Size

Gets the total size of all files in the directory tree in bytes.

public override long Size { get; }

Property Value

Int64

The total size in bytes of all files in the directory and its subdirectories.

Remarks

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.


Method Details

Equals(DirectoryInformation)

Determines whether the specified DirectoryInformation is equal to the current instance.

public bool Equals(DirectoryInformation? other)

Parameters

other DirectoryInformation

The DirectoryInformation to compare with the current instance.

Returns

Boolean

true if the instances are equal; otherwise, false.

Remarks

Equality is based on path, file count, and directory count.


Equals(Object)

Determines whether the specified object is equal to the current instance.

public override bool Equals(object? obj)

Parameters

obj Object

The object to compare with the current instance.

Returns

Boolean

true if the object is a DirectoryInformation with equal values; otherwise, false.


GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

Int32

A hash code based on the directory path, file count, and directory count.


ToString()

Returns a string representation of the directory information.

public override string ToString()

Returns

String

A string in the format: DisplayName (Files=X, Directories=Y, Size=Z)


Operator Details

Equality Operator

Determines whether two DirectoryInformation instances are equal.

public static bool operator ==(DirectoryInformation? left, DirectoryInformation? right)

Parameters

left DirectoryInformation

The first instance to compare.

right DirectoryInformation

The second instance to compare.

Returns

Boolean

true if the instances are equal; otherwise, false.


Inequality Operator

Determines whether two DirectoryInformation instances are not equal.

public static bool operator !=(DirectoryInformation? left, DirectoryInformation? right)

Parameters

left DirectoryInformation

The first instance to compare.

right DirectoryInformation

The second instance to compare.

Returns

Boolean

true if the instances are not equal; otherwise, false.


Examples

Getting Directory Statistics

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}");

Handling Access Denied Scenarios

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}");
}

Comparing Directories

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");
}

Finding Large Directories

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}");
}

Recursive Directory Analysis

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");

Performance Considerations

  • 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

Thread Safety

This class is thread-safe for read operations once constructed. However, construction itself performs I/O operations that block until complete.

See Also

Clone this wiki locally