Skip to content

Commit 62be572

Browse files
committed
Add HasParent extension method on IFileInfo and IDirectoryInfo
1 parent 986dede commit 62be572

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/Elastic.Documentation/Extensions/IFileInfoExtensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public static bool IsSubPathOf(this IFileInfo file, IDirectoryInfo parentDirecto
2525
var parent = file.Directory;
2626
return parent is not null && parent.IsSubPathOf(parentDirectory);
2727
}
28+
29+
/// Checks if <paramref name="file"/> has parent directory <paramref name="parentName"/>
30+
public static bool HasParent(this IFileInfo file, string parentName)
31+
{
32+
var parent = file.Directory;
33+
return parent is not null && parent.HasParent(parentName);
34+
}
2835
}
2936

3037
public static class IDirectoryInfoExtensions
@@ -42,4 +49,20 @@ public static bool IsSubPathOf(this IDirectoryInfo directory, IDirectoryInfo par
4249

4350
return false;
4451
}
52+
53+
/// Checks if <paramref name="directory"/> has parent directory <paramref name="parentName"/>
54+
public static bool HasParent(this IDirectoryInfo directory, string parentName)
55+
{
56+
if (directory.Name == parentName)
57+
return true;
58+
var parent = directory;
59+
do
60+
{
61+
if (parent.Name == parentName)
62+
return true;
63+
parent = parent.Parent;
64+
} while (parent != null);
65+
66+
return false;
67+
}
4568
}

0 commit comments

Comments
 (0)