[Mono-list] Directory.EnumerateFiles() does not enumerate read-only or hidden files

Uli Hertlein uli at xdt.com.au
Fri Mar 11 00:16:38 EST 2011


Hi guys,

the subject sums it up - mono's (2.10.1 and git) System.IO.Directory.EnumerateFiles() does show 
hidden or read-only files; MS .NET does show all files.

The reason for this is that read-only/hidden files do not have the FileAttributes.Normal bit set, 
which comes from the Win32 API (my emphasis):

FILE_ATTRIBUTE_NORMAL:
     A file that does not have other attributes set.
     *This attribute is valid only when used alone.*"

So:
normal    -> FileAttributes.Normal
ro        -> FileAttributes.ReadOnly
hidden    -> FileAttributes.Hidden
hidden/ro -> FileAttributes.Hidden | FileAttributes.ReadOnly

but:
directory/ro -> FileAttributes.Directory | FileAttributes.ReadOnly

(Which seems dumb: 'Normal' is the file type, like 'Directory' or 'Device'; 'ReadOnly', 'Hidden', 
'System', 'Encrypted' are additional attributes.  But that's beside the point.)

The least intrusive way I can see to fix it is to add these flags (and probably Archived,System,etc 
as well) in the various EnumerateFiles/EnumerateFileSystemEntries overloads, like such:

<code>
public static IEnumerable<string> EnumerateFiles (string path, string searchPattern)
{
     EnumerateCheck (path, searchPattern, SearchOption.TopDirectoryOnly);
     return EnumerateKind (path, searchPattern, SearchOption.TopDirectoryOnly, FileAttributes.Normal 
| FileAttributes.ReadOnly | FileAttributes.Hidden);
}

public static IEnumerable<string> EnumerateFileSystemEntries (string path, string searchPattern)
{
     EnumerateCheck (path, searchPattern, SearchOption.TopDirectoryOnly);
     return EnumerateKind (path, searchPattern, SearchOption.TopDirectoryOnly, FileAttributes.Normal 
| FileAttributes.ReadOnly | FileAttributes.Hidden | FileAttributes.Directory);
}
</code>

Thoughts?

Cheers,
/uli

-- 
Ulrich Hertlein
Research and Development   mailto:uli at xdt.com.au
XDT Pty Ltd                http://www.xdt.com.au
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Program.cs
Url: http://lists.ximian.com/pipermail/mono-list/attachments/20110311/c3759b0f/attachment.pl 


More information about the Mono-list mailing list