网上很多人写的很复杂,不知道是什么原因,这里贴一段自己写的代码
private void GetFiles(List<string> AFileList, string Path, string FileExt) { if (!Directory.Exists(Path)) return; DirectoryInfo di = new DirectoryInfo(Path); FileSystemInfo[] fsiArray = di.GetFileSystemInfos(); foreach (FileSystemInfo fsi in fsiArray) { if (fsi.Attributes == FileAttributes.Directory ) { //文件夹递归 GetFiles(AFileList, fsi.FullName, FileExt); } else if ((fsi.Attributes == FileAttributes.Normal) || (fsi.Attributes == FileAttributes.Archive)) { //检查文件扩展名 if (fsi.Extension.ToUpper() == FileExt.ToUpper()) { AFileList.Add(fsi.FullName); } } } }