调用: ListFiles(new DirectoryInfo(source)); /// <summary> /// //递归遍历所有文件包括子文件夹下的文件 并对word excel pdf文件进行复制到目标路径 /// </summary> /// <param name="info"></param> private void ListFiles(FileSystemInfo info) { if (!info.Exists) { return; } DirectoryInfo dir = info as DirectoryInfo; //不是目录 if (dir == null) { return; } FileSystemInfo[] files = dir.GetFileSystemInfos(); for (int i = 0; i < files.Length; i++) { FileInfo file = files[i] as FileInfo; //是文件 if (file != null) { string[] arr = file.Name.Split(new char[] { '.' }); arr[arr.Length - 1] = arr[arr.Length - 1].ToLower(); if (arr[arr.Length - 1] == "doc" || arr[arr.Length - 1] == "docx") { #region 每扫描到一个word文件 存储到lst里面去 FilesModelRepostory fileModel = new FilesModelRepostory(); fileModel.ID = Guid.NewGuid().ToString("N"); fileModel.FileName = file.Name; fileModel.FileType =FileType.Word; AddJob(fileModel); #endregion count++; } if (arr[arr.Length - 1] == "xls" || arr[arr.Length - 1] == "xlsx") { #region 每扫描到一个Excel文件 存储到lst里面去 FilesModelRepostory fileModel = new FilesModelRepostory(); fileModel.ID = Guid.NewGuid().ToString("N"); fileModel.FileName = file.Name; fileModel.FileType = FileType.Excel; AddJob(fileModel); #endregion count++; } if (arr[arr.Length - 1] == "pdf") { #region 每扫描到一个pdf文件 存储到lst里面去 FilesModelRepostory fileModel = new FilesModelRepostory(); fileModel.ID = Guid.NewGuid().ToString("N"); fileModel.FileName = file.Name; fileModel.FileType =FileType.Pdf; AddJob(fileModel); #endregion count++; } } //对于子目录,进行递归调用 else { ListFiles(files[i]); } } }