Table of Contents

Method GetDirectory

Namespace
IOInfoExtensions
Assembly
IOInfoExtensions.dll

GetDirectory(DirectoryInfo, string, bool, bool)

Returns a DirectoryInfo object for a child directory with the specified name.
public static DirectoryInfo GetDirectory(this DirectoryInfo directory, string name, bool resolve = false, bool ignoreCase = false)

Remarks

Creates a new DirectoryInfo object referencing a child directory of the calling object. The newly created DirectoryInfo object will have the given name and be validated against the remaining parameters.

Specifying nested directories in name is supported.

Parameters

directory DirectoryInfo
The calling DirectoryInfo object that will be the parent of the returned DirectoryInfo object.
name string
The name of the child directory to return.
resolve bool
If set to true the child directory's existance will be validated.
ignoreCase bool
If set to true the search for the child directory will be case in-sensitive.

Returns

DirectoryInfo
System.IO.DirectoryInfo referencing the desired child directory.

Examples

Example 1
Get an existing child directory.
var demoRoot = new DirectoryInfo("C:\\Demo");

Console.WriteLine("First level children:");
demoRoot.GetFileSystemInfos().ToList().ForEach(x => Console.WriteLine($"\t{x.FullName}"));

var child = demoRoot.GetDirectory("ChildDir1");
Console.WriteLine($"Child FullName:\n\t{child.FullName}");
Console.WriteLine($"Child Type:\n\t{child.GetType()}");
Console.WriteLine($"Child Exists:\n\t{child.Exists}");

/*
Output:
First level children:
        C:\Demo\ChildDir1
        C:\Demo\ChildDir2
        C:\Demo\ChildFile1.txt
        C:\Demo\ChildFile2.txt
Child FullName:
        C:\Demo\ChildDir1
Child Type:
        System.IO.DirectoryInfo
Child Exists:
        True
*/
$directory = New-Object System.IO.DirectoryInfo 'C:\Demo'

Write-Host "First level children:"
$directory.GetFileSystemInfos().FullName | ForEach-Object { Write-Host "`t$_" }

$child = $directory.GetDirectory('ChildDir1')
Write-Host "Child FullName:`n`t$($child.FullName)"
Write-Host "Child Type:`n`t$($child.GetType().FullName)"
Write-Host "Child Exists:`n`t$($child.Exists)"

<#
Output:
First level children:
        C:\Demo\ChildDir1
        C:\Demo\ChildDir2
        C:\Demo\ChildFile1.txt
        C:\Demo\ChildFile2.txt
Child FullName:
        C:\Demo\ChildDir1
Child Type:
        System.IO.DirectoryInfo
Child Exists:
        True
#>
Example 2
Get a non-existing nested child directory.
var demoRoot = new DirectoryInfo("C:\\Demo");

Console.WriteLine("Children:");
demoRoot.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

var child = demoRoot.GetDirectory("ChildDir3\\InnerChildDir3");
Console.WriteLine($"Child FullName:\n\t{child.FullName}");
Console.WriteLine($"Child Type:\n\t{child.GetType()}");
Console.WriteLine($"Child Exists:\n\t{child.Exists}");

/*
Output:
Children:
        C:\Demo\ChildDir1
        C:\Demo\ChildDir2
        C:\Demo\ChildFile1.txt
        C:\Demo\ChildFile2.txt
        C:\Demo\ChildDir2\ChildFile1.txt
        C:\Demo\ChildDir2\ChildFile2.txt
Child FullName:
        C:\Demo\ChildDir3\InnerChildDir3
Child Type:
        System.IO.DirectoryInfo
Child Exists:
        False
*/
$directory = New-Object System.IO.DirectoryInfo 'C:\Demo'

Write-Host "Children:"
$directory.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

$child = $directory.GetDirectory('ChildDir3\InnerChildDir3')
Write-Host "Child FullName:`n`t$($child.FullName)"
Write-Host "Child Type:`n`t$($child.GetType().FullName)"
Write-Host "Child Exists:`n`t$($child.Exists)"

<#
Output:
Children:
        C:\Demo\ChildDir1
        C:\Demo\ChildDir2
        C:\Demo\ChildFile1.txt
        C:\Demo\ChildFile2.txt
        C:\Demo\ChildDir2\ChildFile1.txt
        C:\Demo\ChildDir2\ChildFile2.txt
Child FullName:
        C:\Demo\ChildDir3\InnerChildDir3
Child Type:
        System.IO.DirectoryInfo
Child Exists:
        False
#>
Example 3
Resolve a child directory using case sensitivity.
var demoRoot = new DirectoryInfo("C:\\Demo");

Console.WriteLine("First level children:");
demoRoot.GetFileSystemInfos().ToList().ForEach(x => Console.WriteLine($"\t{x.FullName}"));

try
{
    _ = demoRoot.GetDirectory("childdir1", true, false);
}
catch (Exception ex)
{
    var stringArgs = new[] {
        ex.GetType().ToString(),
        ex.Message,
        ex.StackTrace.Substring(0, ex.StackTrace.IndexOf(Environment.NewLine))
    };

    Console.Error.WriteLine(string.Format("\nError: {0}: {1}\n{2}\n", stringArgs));
}

/*
Output:
First level children:
        C:\Demo\ChildDir1
        C:\Demo\ChildDir2
        C:\Demo\ChildFile1.txt
        C:\Demo\ChildFile2.txt

Error: System.IO.DirectoryNotFoundException: A child named 'childdir1' already exists but with a different case: ChildDir1.
    at IOInfoExtensions.DirectoryInfoExtensions.GetDirectory(DirectoryInfo directory, String name, Boolean resolve, Boolean ignoreCase)
*/
$directory = New-Object System.IO.DirectoryInfo 'C:\Demo'

Write-Host "First level children:"
$directory.GetFileSystemInfos().FullName | ForEach-Object { Write-Host "`t$_" }

try
{
    $null = $directory.GetDirectory('childdir1', $true, $false)
}
catch
{
    $stringArgs = @(
        $_.Exception.GetType().ToString(),
        $_.Exception.Message,
        $_.Exception.StackTrace.Substring(0, $_.Exception.StackTrace.IndexOf([Environment]::NewLine))
    )

    Write-Host ("`nError: {0}: {1}`n{2}`n" -f $stringArgs)
    $error.Clear()
}

<#
Output:
First level children:
        C:\Demo\ChildDir1
        C:\Demo\ChildDir2
        C:\Demo\ChildFile1.txt
        C:\Demo\ChildFile2.txt

Error: System.IO.DirectoryNotFoundException: A child named 'childdir1' already exists but with a different case: ChildDir1.
    at IOInfoExtensions.DirectoryInfoExtensions.GetDirectory(DirectoryInfo directory, String name, Boolean resolve, Boolean ignoreCase)
#>

Exceptions

ArgumentException
If the name parameter value is null, empty, or just whitespace.
Exception
If the name matched multiple child items. This will happen if a wildcard was passed as part of the name.
DirectoryNotFoundException
If the name resolves to a child file, resolve is set to true and the child directory does not exist, or if ignoreCase is set to false and the casing doesn't match.