c# 遍历目录


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

            List<String> list = new List<string>();
            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<String> listSubFolder = TraverseDirector(fsinfo.FullName, isTraveSubDirFlag, isFilterSuffix, suffix);//递归调用
                        list.AddRange(listSubFolder);
                    }
                }
                else
                {
                    //Console.WriteLine(fsinfo.FullName);//输出文件的全部路径
                    list.Add(fsinfo.FullName);
                }
            }
            return list;
        }
        
posted @ 2019-03-22 17:54  scott_h  阅读(476)  评论(0编辑  收藏  举报