Class DirectoryInfoExtensions
- Namespace
- Garyon.Extensions
- Assembly
- Garyon.dll
Contains extensions for the DirectoryInfo class.
public static class DirectoryInfoExtensions
- Inheritance
-
DirectoryInfoExtensions
- Inherited Members
Methods
BoundRecursiveParent(DirectoryInfo, int)
Gets the ancestor directory of this directory, at a specified number of levels up in the directory tree. If the current directory's depth is less than the provided number of levels to walk up, the returned directory is the root directory.
public static DirectoryInfo BoundRecursiveParent(this DirectoryInfo directory, int levels = 1)
Parameters
directoryDirectoryInfoThe directory to walk up from.
levelsintThe number of levels to walk up the directory tree. 0 returns the current directory.
Returns
- DirectoryInfo
The ancestor directory at the given level, or the root directory.
CombinePath(DirectoryInfo, string)
Returns a path that combines the directory's full path with the specified next part of the path.
public static string CombinePath(this DirectoryInfo directory, string next)
Parameters
directoryDirectoryInfonextstringThe next part of the path to append after the directory's path. Leading separators are not required.
Returns
- string
The combined path using Combine(string, string).
Depth(DirectoryInfo)
Gets the depth of this directory, meaning the number of directories between the root and this directory.
public static int Depth(this DirectoryInfo directory)
Parameters
directoryDirectoryInfoThe directory whose depth to get.
Returns
- int
The number of parent directories that must be traversed before reaching the file system root.
EnumerateParents(DirectoryInfo)
Enumerates all parent directories.
public static IEnumerable<DirectoryInfo> EnumerateParents(this DirectoryInfo directory)
Parameters
directoryDirectoryInfo
Returns
- IEnumerable<DirectoryInfo>
A lazily evaluated collection of directories, with the first being the direct parent, and the last being the root of the path.
Remarks
This only includes all types returned from Parent recursively. It does not include implemented interfaces.
ExploreDeepestSingleDirectory(DirectoryInfo)
Traverses the single subdirectory recursively and finds the deepest subdirectory that does not contain only a single subdirectory. The resulting subdirectory will contain more one file system objects, or a single file, or nothing.
public static DirectoryInfo ExploreDeepestSingleDirectory(this DirectoryInfo directoryInfo)
Parameters
directoryInfoDirectoryInfoThe directory whose deepest single directory to get. It does not matter how many other directories are contained in the parent directory of the given directory. For instance, the given directory is D, and P is the parent directory of D. Whether P only contains a single directory, i.e. D, does not prevent the exploration of the subdirectories of D.
Returns
- DirectoryInfo
The deepest directory that does not contain a single directory, starting from the given directory. For example, assume the starting directory is D, D only contains the directory E, E only contains the directory F and F contains two other directories. This method will return F, which is the last directory that was the only option to explore into.
File(DirectoryInfo, string)
Gets the FileInfo of a file within the given directory, or a subdirectory of it.
public static FileInfo File(this DirectoryInfo directory, string fileName)
Parameters
directoryDirectoryInfofileNamestringThe name of the file to get. Nested subdirectories are allowed, if the path is properly separated.
Returns
MoveTo(DirectoryInfo, DirectoryInfo)
Moves a directory to the given destination directory.
public static void MoveTo(this DirectoryInfo directory, DirectoryInfo destination)
Parameters
directoryDirectoryInfoThe directory to move.
destinationDirectoryInfoThe target directory to move the source directory to.
MoveUp(DirectoryInfo, int)
Moves the given subdirectory a number of levels up, based on RecursiveParent(DirectoryInfo, int). If the returned directory from that method is null, an exception is thrown.
public static DirectoryInfo MoveUp(this DirectoryInfo directory, int levels = 1)
Parameters
directoryDirectoryInfoThe directory to move up.
levelsintThe number of levels to walk up the directory tree. 0 returns the current directory.
Returns
- DirectoryInfo
The result of BoundRecursiveParent(DirectoryInfo, int).
Exceptions
- InvalidOperationException
Thrown when RecursiveParent(DirectoryInfo, int) returns null.
MoveUpBound(DirectoryInfo, int)
Moves the given subdirectory a number of levels up, based on BoundRecursiveParent(DirectoryInfo, int).
public static DirectoryInfo MoveUpBound(this DirectoryInfo directory, int levels = 1)
Parameters
directoryDirectoryInfoThe directory to move up.
levelsintThe number of levels to walk up the directory tree. 0 returns the current directory.
Returns
- DirectoryInfo
The result of BoundRecursiveParent(DirectoryInfo, int).
RecursiveParent(DirectoryInfo, int)
Gets the ancestor directory of this directory, at a specified number of levels up in the directory tree. If the current directory's depth is less than the provided number of levels to walk up, null is returned.
public static DirectoryInfo? RecursiveParent(this DirectoryInfo directory, int levels = 1)
Parameters
directoryDirectoryInfoThe directory to walk up from.
levelsintThe number of levels to walk up the directory tree. 0 returns the current directory.
Returns
- DirectoryInfo
The ancestor directory at the given level, or null if the given directory's depth was less than the given number of levels to walk up.
Subdirectory(DirectoryInfo, string)
Gets the DirectoryInfo of a subdirectory within the given directory.
public static DirectoryInfo Subdirectory(this DirectoryInfo directory, string subdirectoryName)
Parameters
directoryDirectoryInfosubdirectoryNamestringThe name of the subdirectory to get. Nested subdirectories are allowed, if the path is properly separated.
Returns
TotalSize(DirectoryInfo)
Gets the total size of the files contained in this directory, only accounting for the directly contained files, and not ones in subdirectories.
public static long TotalSize(this DirectoryInfo directory)
Parameters
directoryDirectoryInfoThe directory that contains the files.
Returns
- long
The total size of the files directly contained in this directory only, in bytes as returned for each file by Length.
TotalSizeDeep(DirectoryInfo)
Gets the total size of the files contained in this directory, traversing subdirectories and including their total sizes.
public static long TotalSizeDeep(this DirectoryInfo directory)
Parameters
directoryDirectoryInfoThe directory whose total file size to get.
Returns
- long
The total size of the files contained in this and nested subdirectories, in bytes as returned for each file by Length and via TotalSize(DirectoryInfo) for each directory.
TryDelete(DirectoryInfo, bool)
Attempts to delete the given directory, catching any thrown exception from the operation and returning whether the operation succeeded.
public static bool TryDelete(this DirectoryInfo directory, bool recursive)
Parameters
directoryDirectoryInfoThe directory to delete.
recursivebooltrue to delete all the contents of the directory and subdirectories; false to delete only the directory itself if it is empty, following Delete(bool).
Returns
TryGetSingleSubdirectory(DirectoryInfo?)
Gets the single subdirectory contained in the given directory.
public static DirectoryInfo? TryGetSingleSubdirectory(this DirectoryInfo? directoryInfo)
Parameters
directoryInfoDirectoryInfo
Returns
- DirectoryInfo
The single subdirectory inside the given directory, otherwise null.
Remarks
For the method to return a non-null result, the directory must contain exactly 1 subdirectory and no files.