c# 遍历目录

2021-06-11 03:07

阅读:679

标签:lis   递归   else   director   search   lin   reac   write   arch   


 public static List TraverseDirector(string dir, bool isTraveSubDirFlag, bool isFilterSuffix, string suffix)
        {
            string suffix_ = "*.*";
            if (isFilterSuffix)
            {
                suffix_ = suffix;
            }

            List list = new List();
            DirectoryInfo d = new DirectoryInfo(dir);

            FileSystemInfo[] fsinfos = d.GetFileSystemInfos(suffix_, SearchOption.TopDirectoryOnly);//filter file format
            //FileSystemInfo[] fsinfos = d.GetFileSystemInfos();//
            foreach (FileSystemInfo fsinfo in fsinfos)
            {
                if (fsinfo is DirectoryInfo)     //判断是否为文件夹
                {
                    if (isTraveSubDirFlag)
                    {
                        List listSubFolder = TraverseDirector(fsinfo.FullName, isTraveSubDirFlag, isFilterSuffix, suffix);//递归调用
                        list.AddRange(listSubFolder);
                    }
                }
                else
                {
                    //Console.WriteLine(fsinfo.FullName);//输出文件的全部路径
                    list.Add(fsinfo.FullName);
                }
            }
            return list;
        }
        

c# 遍历目录

标签:lis   递归   else   director   search   lin   reac   write   arch   

原文地址:https://www.cnblogs.com/scotth/p/10579880.html


评论


亲,登录后才可以留言!