方法1 通过 位与
static void Main(string[] Args)
{
//假设扫描C:\Test中——
string[] hiddenfiles = Directory.GetFiles("C:\\Test\\", "*.*", SearchOption.AllDirectories);
foreach (var item in hiddenfiles)
{
if ((new FileInfo(item).Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) //必须进行与运算,因为默认文件是“Hidden”+归档(二进制11)。而Hidden是10.因此与运算才可以判断
{
Console.WriteLine(item);
}
}
}
方法二 通过位或
DirectoryInfo directoryInfo = new DirectoryInfo(@"D:\TEST"); FileInfo[] fileList = directoryInfo.GetFiles("*.*", SearchOption.AllDirectories); foreach (FileInfo File in fileList) { if ((File.Attributes | FileAttributes.Hidden) == File.Attributes) { this.listBoxFileList.Items.Add(File.FullName); } }
时间: 2024-10-10 15:39:15