If I do this:
var entries = new DirectoryInfo(@"C:\Folder1")
.GetFileSystemInfos("test*", SearchOption.AllDirectories);
when I have a folder structure like this, where the "HiddenFolder" is hidden but the "test.txt" file is not hidden:
C:\Folder1\
C:\Folder1\HiddenFolder\
C:\Folder1\HiddenFolder\test.txt
Then how can I code to NOT get the "test.txt" file?
(You might think that GetFileSystemInfos would skip hidden folders, but it doesn't.)
Here is a partial solution
This code strips out hidden files and folders, but doesn't handle a non-hidden file inside a hidden folder!
var includeLocalFolderNames = true; // to include folder names in the list
var hiddenFolders = new List<FileSystemInfo>();
return entries
.Where(entry =>
{
// skip if this entry is hidden, or it is in a hidden folder
var isHidden = (entry.Attributes & FileAttributes.Hidden) != 0;
if (isHidden)
{
hiddenFolders.Add(entry);
return false;
}
return !hiddenFolders.Any(fsi => entry.FullName.StartsWith(fsi.FullName));
})
.Where(entry =>
{
// include folder names if requested...
if (includeLocalFolderNames) return true;
return (entry.Attributes & FileAttributes.Directory) == 0;
})
.Select(entry => {
// do something...
return entry.Name;
});
Certainly not the most optimal and/or elegant solution.