Skip to content

Commit bf3503b

Browse files
committed
fix: DirectoryInfo.GetFiles may raise System.Reflection.TargetInvocationException (#2117)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 61cc276 commit bf3503b

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/App.Extensions.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,32 @@ public static class DirectoryInfoExtension
2929
{
3030
public static void WalkFiles(this DirectoryInfo dir, Action<string> onFile, int maxDepth = 4)
3131
{
32-
foreach (var file in dir.GetFiles())
33-
onFile(file.FullName);
32+
try
33+
{
34+
var options = new EnumerationOptions()
35+
{
36+
IgnoreInaccessible = true,
37+
RecurseSubdirectories = false,
38+
};
39+
40+
foreach (var file in dir.GetFiles("*", options))
41+
onFile(file.FullName);
3442

35-
if (maxDepth > 0)
43+
if (maxDepth > 0)
44+
{
45+
foreach (var subDir in dir.GetDirectories("*", options))
46+
{
47+
if (subDir.Name.StartsWith(".", StringComparison.Ordinal) ||
48+
subDir.Name.Equals("node_modules", StringComparison.OrdinalIgnoreCase))
49+
continue;
50+
51+
WalkFiles(subDir, onFile, maxDepth - 1);
52+
}
53+
}
54+
}
55+
catch
3656
{
37-
foreach (var subDir in dir.GetDirectories())
38-
WalkFiles(subDir, onFile, maxDepth - 1);
57+
// Ignore exceptions.
3958
}
4059
}
4160

0 commit comments

Comments
 (0)