如果我这样做:
var entries = new DirectoryInfo(@"C:\Folder1")
.GetFileSystemInfos("test*", SearchOption.AllDirectories);
当我有这样的文件夹结构时,其中“HiddenFolder”被隐藏但“test.txt”文件未被隐藏:
C:\Folder1\
C:\Folder1\HiddenFolder\
C:\Folder1\HiddenFolder\test.txt
那么我怎样才能编码不获取“test.txt”文件?
(您可能认为 GetFileSystemInfos 会跳过隐藏文件夹,但事实并非如此。)
这是部分解决方案
此代码去除隐藏文件和文件夹,但不处理隐藏文件夹中的非隐藏文件!
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;
});