查询文件夹以及子文件夹下面的文件

 //获得院校相册图片,从college文件夹下查询所有子文件夹
        public List<string> ListFilesColleges(FileSystemInfo info)
        {
            List<string> image_list = new List<string>();
            if (!info.Exists) return null;
            DirectoryInfo dir = info as DirectoryInfo;
            //不是目录
            if (dir == null) return null;
            FileSystemInfo[] files = dir.GetFileSystemInfos();
            for (int i = 0; i < files.Length; i++)
            {
                string son_folder_name = files[i].Name;//获取文件夹名称
                DirectoryInfo dir_college = files[i] as DirectoryInfo;
                FileSystemInfo[] file_college_name = dir_college.GetFileSystemInfos();//子文件夹里面的文件
                if (file_college_name.Length > 0)
                {
                    FileInfo file = file_college_name[0] as FileInfo;
                    string filename = file.Name;
                    string fileEx = filename.Substring(filename.LastIndexOf(".") + 1).ToLower();//后缀jpg jpeg gif bmp
                    if (fileEx == "jpg" || fileEx == "jpeg" || fileEx == "gif" || fileEx == "bmp")
                    {
                        image_list.Add(son_folder_name + "/" + file.Name);
                    }
                }
            }
            return image_list;
        }

原文地址:https://www.cnblogs.com/tongdengquan/p/6090591.html