When DirectoryInfo.Exists is false, DirectoryInfo.Attributes == -1.
However, DirectoryInfo.Attributes is an enum (FileAttributes) with the [Flags] attribute applied. So when you do DirectoryInfo.Attributes.HasFlag(any flag value here) on a directory that doesn't exist, HasFlag(...) always returns true. But since the directory does not exist, a more correct result would be false.
Of course we can check Exists before Attributes.HasFlag(), but that seems more like a workaround and something that should be handled by the library.
Rather than setting DirectoryInfo.Attributes == -1, perhaps make it == 0 (zero; FileAttributes.None)
Reproduction Steps
DirectoryInfo dir = new DirectoryInfo(@"J:\hot-potato\something-that-will-never-exist");
bool exists = dir.Exists; // false
bool isEncrypted = dir.Attributes.HasFlag(FileAttributes.encrypted); // true
Expected behavior
DirectoryInfo.Attributes.HasFlag(...) should return false if the directory does not exist.
Description
When DirectoryInfo.Exists is
false
, DirectoryInfo.Attributes == -1.However, DirectoryInfo.Attributes is an enum (FileAttributes) with the [Flags] attribute applied. So when you do DirectoryInfo.Attributes.HasFlag(any flag value here) on a directory that doesn't exist, HasFlag(...) always returns true. But since the directory does not exist, a more correct result would be false.
Of course we can check Exists before Attributes.HasFlag(), but that seems more like a workaround and something that should be handled by the library.
Rather than setting DirectoryInfo.Attributes == -1, perhaps make it == 0 (zero; FileAttributes.None)
Reproduction Steps
Expected behavior
DirectoryInfo.Attributes.HasFlag(...) should return false if the directory does not exist.
Actual behavior
It returns true.
Regression?
No idea.
Known Workarounds
Check the Directory.Info.Exists property fist.
Configuration
dotnet 8.0 on Windows 11
Other information
No response