在c#中过滤通过System.IO.Directory.GetDirectories 方法获取的是所有的子目录和文件中的系统隐藏的文件(夹)的方法

时间:2021-04-14 12:26:46
  //读取目录 下的所有非隐藏文件夹或文件   
        public  List<FileItem> GetList(string path)
        {      
            int i;
            string[] folders = Directory.GetDirectories(path);
            string[] files = Directory.GetFiles(path);
            List<FileItem> list = new List<FileItem>();
            foreach (string s in folders)
            {

                FileItem item = new FileItem();
                DirectoryInfo di = new DirectoryInfo(s);
                string[] w = di.Name.Split(new char[] { '#' });
                if (w.Length == 1)
                {
                    item.Name = di.Name;
                }
                else
                {
                    item.Name="";
                    for(i=1;i<w.Length;i++)
                        item.Name=item.Name+w[i];
                }
                item.FullName =rlocaltourlconver(di.FullName);      
                item.CreationDate = di.CreationTime;
                item.Size =(float)FileSize(s)/(1024);
                item.IsFolder = true;
                if ((new FileInfo(s).Attributes & FileAttributes.Hidden) ==0)//过滤隐藏文件夹
                {
                    list.Add(item);
                }
               
            }
            foreach (string s in files)
            {
                FileItem item = new FileItem();
                FileInfo fi = new FileInfo(s);
                string[] w = fi.Name.Split(new char[] { '#' });
                if (w.Length == 1)
                {
                    item.Name = fi.Name;
                }
                else
                {
                    item.Name = "";
                    for (i = 1; i < w.Length; i++)
                        item.Name = item.Name + w[i];
                }
                item.FullName = rlocaltourlconver(fi.FullName);        
               
                item.CreationDate = fi.CreationTime;
                item.IsFolder = true;
                item.Size = (float)fi.Length/(1024);                  
                item.FileCount = 0;                     
                if ((new FileInfo(s).Attributes & FileAttributes.Hidden) == 0)//过滤隐藏文件
                {
                    list.Add(item);
                }
            }      
            return list;
        }

参考链接:1,2