dotnet/runtime

Reconsider the exception type thrown when trying to open a directory as file

オープン

#70,275 opened on 2022/06/06

 (6 件のコメント) (0 件のリアクション) (0 人の担当者)C# (5,445 件のフォーク)batch import
area-System.IOhelp wanted

Repository metrics

Stars
 (17,886 個のスター)
PR merge metrics
 (平均マージ 12d 11h) (30d で 661 merged PRs)

説明

In FileSystem.Windows, as well as SafeFileHandle.Windows we have this comment:

https://github.com/dotnet/runtime/blob/6a56aa3512c912df602bf074dc925b77f3895b24/src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.Windows.cs#L216-L224

However, when testing with native code on Windows 10, calling CreateFileA returns ERROR_PATH_NOT_FOUND for either C:\ or other normal directories.

So this causing behavior inconsistency on managed API again: when calling File.Open on C:\, it throws UnauthorizedException. When calling with other directories, it throws PathNotFoundException.

I didn't find any test for exceptions in System.IO.FileSystem.

Changing the exception thrown is technically breaking, but helps for consistency. Changing to an exception is already used for other circumstances won't break much.

We need to also examine Windows 7 and 8 behavior and the behavior of CreateFile2 and NtCreateFile. (Or if you can directly check it through Windows source code)

コントリビューターガイド