如何过滤c#中的不可见文件?

时间:2016-02-10 10:55:47

标签: c#

我想在C#中查找文件夹中的所有不可见文件。我可以枚举文件

var files = from file in 
   Directory.EnumerateFiles(@"c:\", "*.txt", SearchOption.AllDirectories)
select new
{
File = file
};

2 个答案:

答案 0 :(得分:1)

您可以使用FileInfo类检查文件属性:

            FileInfo f = new FileInfo(path);
            if ((f.Attributes & FileAttributes.Hidden) != 0)

答案 1 :(得分:-2)

来自previous SO post

DirectoryInfo directory = new DirectoryInfo(@"C:\");
FileInfo[] files = directory.GetFiles();

var filtered = files.Where(f => f.Attributes.HasFlag(FileAttributes.Hidden));

foreach (var f in filtered)
{
    Debug.WriteLine(f);
}