不显示在列表视图目录中的隐藏文件 [英] Don't show hidden files in a directory listview

查看:135
本文介绍了不显示在列表视图目录中的隐藏文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code生成我的计算机列表视图目录。当我打开C目录它显示$ RECYCLE.BIN。反正有它在那里不显示这些文件?

 的foreach(在文件夹中的字符串DIR)
{
    dirData [0] = dir.ToString();
    dirData [1] = dir.Length.ToString();
    dirData [2] = File.GetLastAccessTime(DIR)的ToString();
    ListViewItem的LV =新的ListViewItem(dirData,imageList1.Images.Count - 1);
    lv.Tag = DIR;    imageList1.Images.Add(BlackFox.Win32.Icons.IconFromExtensionShell(,BlackFox.Win32.Icons.SystemIconSize.Small).ToBitmap()。*);    listView1.SmallImageList = imageList1;
    listView1.Items.Add(LV);
}


解决方案

像这样的东西应该工作

  VAR文件夹= Directory.GetDirectories(@C:\\)
。凡(D = GT;新DirectoryInfo的(D).Attributes.HasFlag(FileAttributes.System | FileAttributes.Hidden)!);的foreach(在文件夹中的字符串DIR)
{
   .....

让我对答案扩大,因为有些人可能觉得很难理解,这会忽略 SystemHidden directrories。

例如:

C:\\\\ $ RECYCLE.BIN

C:\\\\的Documents and Settings

C:\\\\的System Volume Information

如果你想忽略所有的隐藏的目录,你可以简单地忽略从LINQ语句 FileAttributes.System 属性,但是这会导致丢失的目录,比如 ProgramData 的ListView 这我不知道你想要的。

I have code that generates a listview directory of my computer. When I open the C directory it shows $RECYCLE.Bin. Is there anyway to have it where it doesn't show these files?

foreach (string dir in folders)
{
    dirData[0] = dir.ToString();
    dirData[1] = dir.Length.ToString();
    dirData[2] = File.GetLastAccessTime(dir).ToString();
    ListViewItem lv = new ListViewItem(dirData, imageList1.Images.Count - 1);
    lv.Tag = dir;

    imageList1.Images.Add(BlackFox.Win32.Icons.IconFromExtensionShell(".*", BlackFox.Win32.Icons.SystemIconSize.Small).ToBitmap());

    listView1.SmallImageList = imageList1;
    listView1.Items.Add(lv);
}

解决方案

Something like this should work

var folders = Directory.GetDirectories(@"C:\")
.Where(d => !new DirectoryInfo(d).Attributes.HasFlag(FileAttributes.System | FileAttributes.Hidden));

foreach (string dir in folders)
{
   .....

Let me expand on the answer as some people may find it difficult to understand, This will ignore SystemHidden directrories.

ie:

"C:\\$Recycle.Bin"

"C:\\Documents and Settings"

"C:\\System Volume Information"

If you want to ignore all hidden directories you can simply omit the FileAttributes.System attribute from the linq statement but this would result in losing directories like ProgramData in your ListView which I'm not sure you want.

这篇关于不显示在列表视图目录中的隐藏文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆