Skip to content

Commit

Permalink
Don't crash when trying to get the contents of an unauthorized direct…
Browse files Browse the repository at this point in the history
…ory, just return nothing instead
  • Loading branch information
matt-edmondson committed Mar 26, 2024
1 parent ff02f05 commit af2fa5d
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions StrongPaths/AnyDirectoryPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@ public record AnyDirectoryPath : StrongPathAbstract<AnyDirectoryPath, IsPath, Is
public static Collection<AnyAbsolutePath> GetContents(AnyDirectoryPath directory)
{
var contents = new Collection<AnyAbsolutePath>();
foreach (string path in Directory.GetFileSystemEntries(directory))
try
{
if (File.Exists(path))
foreach (string path in Directory.GetFileSystemEntries(directory))
{
contents.Add((AbsoluteFilePath)path);
}
else if (Directory.Exists(path))
{
contents.Add((AbsoluteDirectoryPath)path);
if (File.Exists(path))
{
contents.Add((AbsoluteFilePath)path);
}
else if (Directory.Exists(path))
{
contents.Add((AbsoluteDirectoryPath)path);
}
}
}
catch (UnauthorizedAccessException)
{
// Ignore
}

return contents;
}
Expand Down

0 comments on commit af2fa5d

Please sign in to comment.