Method GetFile
- Namespace
- IOInfoExtensions
- Assembly
- IOInfoExtensions.dll
GetFile(DirectoryInfo, string, bool, bool)
Returns a
FileInfo
object for the child file with the specified name.public static FileInfo GetFile(this DirectoryInfo directory, string name, bool resolve = false, bool ignoreCase = false)
Remarks
Creates a new FileInfo
object referencing a child file of the calling object. The newly created
FileInfo
object will have the given name and be validated against the remaining parameters.
Specifying nested files in name is supported.
Parameters
directory
DirectoryInfo- The calling
DirectoryInfo
object that will be the parent of the returnedFileInfo
object. name
string- The name of the child file to return.
resolve
bool- If set to true the child file's existance will be validated.
ignoreCase
bool- If set to true the search for the child file will be case in-sensitive.
Returns
- FileInfo
System.IO.FileInfo
referencing the desired child file.
Examples
Example 1
Get an existing child file.
var demoRoot = new DirectoryInfo("C:\\Demo");
Console.WriteLine("First level children:");
demoRoot.GetFileSystemInfos().ToList().ForEach(x => Console.WriteLine($"\t{x.FullName}"));
var child = demoRoot.GetFile("ChildFile1.txt");
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\ChildFile1.txt
Child Type:
System.IO.FileInfo
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.GetFile('ChildFile1.txt')
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\ChildFile1.txt
Child Type:
System.IO.FileInfo
Child Exists:
True
#>
Example 2
Get a non-existing nested child file.
var demoRoot = new DirectoryInfo("C:\\Demo");
Console.WriteLine("Children:");
demoRoot.GetFileSystemInfos("*", SearchOption.AllDirectories)
.ToList()
.ForEach(x => Console.WriteLine($"\t{x.FullName}"));
var child = demoRoot.GetFile("ChildDir3\\ChildFile3.txt");
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\ChildFile3.txt
Child Type:
System.IO.FileInfo
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.GetFile('ChildDir3\ChildFile3.txt')
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\ChildFile3.txt
Child Type:
System.IO.FileInfo
Child Exists:
False
#>
Example 3
Resolve a child file 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.GetFile("childfile1.txt", 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.FileNotFoundException: A child named 'childfile1.txt' already exists but with a different case: ChildFile1.txt.
at IOInfoExtensions.DirectoryInfoExtensions.GetFile(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.GetFile('childfile1.txt', $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.FileNotFoundException: A child named 'childfile1.txt' already exists but with a different case: ChildFile1.txt.
at IOInfoExtensions.DirectoryInfoExtensions.GetFile(DirectoryInfo directory, String name, Boolean resolve, Boolean ignoreCase)
#>
Exceptions
- ArgumentException
- If the name 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.
- FileNotFoundException
- If the name resolves to a child directory, resolve is set to true and the child file does not exist, or if ignoreCase is set to false and the casing doesn't match.