Skip to content

Improve exception message when trying to open a directory as a file. #100720

Closed

Description

@bsj0006

Description

If you mistakenly pass a directory as the sourceArchiveFileName parameter for ZipFile.ExtractToDirectory(), you receive an UnauthorizedAccessException.

Reproduction Steps

ZipFile.ExtractToDirectory("/temp/thisIsADirectory", "/temp/alsoADirectory")

Expected behavior

Based on the current docs, I would assume the exception would be a NotSupportedException or InvalidDataException with a message like "[sourceArchiveFileName] is not a valid zip archive".

Actual behavior

You get an UnauthorizedAccessException with the message "Access to the path '[sourceArchiveFileName]' is denied" regardless of permissions on the directory.

Regression?

.NET 8, 7, & 6

Known Workarounds

No response

Configuration

.NET 8
Windows 11 x64 and mcr.microsoft.com/dotnet/runtime:8.0 Docker image

Other information

No response

Activity

  1. dotnet-policy-service commented on Apr 6, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-io
    See info in area-owners.md if you want to be subscribed.

  2. added
    untriaged
    on Apr 6, 2024
  3. changed the title [-]Misleading exception for ZipFIle.ExtractToDirectory[/-] [+]Misleading exception for ZipFile.ExtractToDirectory[/+] on Apr 6, 2024
  4. KalleOlaviNiemitalo commented on Apr 8, 2024

    @KalleOlaviNiemitalo

    Perhaps FileStream could just change the Exception.Message of the UnauthorizedAccessException to say that it's a directory, when it gets an EISDIR error from the operating system. Changing the type of the exception seems more risky.

  5. ericstj commented on Jul 24, 2024

    @ericstj
    Member

    I don't expect we can change the exception type, but perhaps a better error message is possible when a directory path is provided to open. Moving the suggestion to IO.

  6. dotnet-policy-service commented on Jul 24, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-io
    See info in area-owners.md if you want to be subscribed.

  7. jozkee commented on Jul 26, 2024

    @jozkee
    Member

    We're open to suggestions for improving the exception message. Some IO APIs document this behavior e.g. File.Open, some don't (e.g. FileStream.ctors, although it should).

    ZipFile.ExtractToDirectory could also lift the exception documentation, so it becomes clear for its users.

  8. added this to the Future milestone on Jul 26, 2024
  9. removed
    untriaged
    on Jul 26, 2024
  10. dotnet-policy-service commented on Oct 23, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-io-compression
    See info in area-owners.md if you want to be subscribed.

  11. dotnet-policy-service commented on Oct 23, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-io
    See info in area-owners.md if you want to be subscribed.

  12. changed the title [-]Misleading exception for ZipFile.ExtractToDirectory[/-] [+]Improve exception message when trying to open a directory as a file.[/+] on Oct 23, 2025
  13. self-assigned this
    on Nov 17, 2025
  14. added a commit that references this issue on Nov 28, 2025
    2e0cb6b
  15. locked and limited conversation to collaborators on Dec 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions