You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When you get the FileInfo of a file that does not exist, IsReadOnly is true while the Exists property is false.
To Reproduce
FileSystem = new MockFileSystem();
var fileInfo = FileSystem.FileInfo.New("C:\\Folder\\Donuts.sldasm");
var isReadOnly = fileInfo.IsReadOnly;
Expected behavior
According to the docs, IsReadOnly should throw a FileNotFoundException when the file does not exist.
Additional context
I've looked through previous issues but could not find any mentioning this.
In my code, I was checking the FileInfo attributes to see if it was read-only. If a file does not exist, the attributes return -1, which, when you bitwise AND with any value, returns the positive value, apparently. Maybe that's happening internally as well.
I created a console app in .NET 7 to let me compare System.IO with System.IO.Abstractions. It looks like the official docs are not the same as the current .NET code because IsReadOnly just returns true when the file does not exist:
When the file does exist:
So is this a .NET issue? That doesn't seem right 😄
@brinkdinges : I tested this issue in Testably/Testably.Abstractions#233:
The tests run on Windows, MacOS and Linux and a wide range of .NET versions. In all circumstances IsReadOnly returns true when it does not exist.
I agree with you, that it looks strange, but as we try to mimic the real file system, this is the intended behaviour...
Thanks for checking it out. When I reread the docs, they probably only throw a FileNotFoundException when setting the property, not when getting it. I've sent in feedback on that page.
Activity
fgreinacher commentedon Feb 8, 2023
Thanks for reporting! PRs welcome 🤗
brinkdinges commentedon Feb 9, 2023
I created a console app in .NET 7 to let me compare System.IO with System.IO.Abstractions. It looks like the official docs are not the same as the current .NET code because IsReadOnly just returns true when the file does not exist:
When the file does exist:

So is this a .NET issue? That doesn't seem right 😄
vbreuss commentedon Feb 9, 2023
@brinkdinges : I tested this issue in Testably/Testably.Abstractions#233:
The tests run on Windows, MacOS and Linux and a wide range of .NET versions. In all circumstances
IsReadOnlyreturnstruewhen it does not exist.I agree with you, that it looks strange, but as we try to mimic the real file system, this is the intended behaviour...
coverage: add test for isreadonly on missing fileinfo (#233)
brinkdinges commentedon Feb 9, 2023
Thanks for checking it out. When I reread the docs, they probably only throw a FileNotFoundException when setting the property, not when getting it. I've sent in feedback on that page.
fix: Fixed typos in test names, variables and method parameter name (#…
fgreinacher commentedon Feb 14, 2023
Thanks for investigating. I think we can close this then. Feel free to reopen if I missed something.