Skip to content

Commit

Permalink
Directory.Delete: prefer DirectoryNotFoundException over Unauthorized…
Browse files Browse the repository at this point in the history
…Access IOException.
  • Loading branch information
tmds committed Dec 4, 2021
1 parent f179b76 commit 9fdb42b
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,14 @@ private static bool RemoveEmptyDirectory(string fullPath, bool topLevel = false,
case Interop.Error.EACCES:
case Interop.Error.EPERM:
case Interop.Error.EROFS:
// Prefer throwing DirectoryNotFoundException over UnauthorizedAccess IOException.
if (topLevel && !DirectoryExists(fullPath, out Interop.ErrorInfo existErr) && existErr.Error == Interop.Error.ENOENT)
{
throw Interop.GetExceptionForIoErrno(Interop.Error.ENOENT.Info(), fullPath, isDirectory: true);
}
throw new IOException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, fullPath));
case Interop.Error.EISDIR:
throw new IOException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, fullPath)); // match Win32 exception
throw new IOException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, fullPath));
case Interop.Error.ENOENT:
// When we're recursing, don't throw for items that go missing.
if (!topLevel)
Expand Down

0 comments on commit 9fdb42b

Please sign in to comment.