日期:2014-05-17 浏览次数:20939 次
public static void ListAllFiles(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) { //是否是一个word文件 if (file.Extension == ".doc" || file.Extension == ".docx") { //这里是拷贝到C盘,可以换做其他处理 if (File.Exists("C:/test/" + file.Name)) { file.CopyTo("C:/test/" + Guid.NewGuid() + file.Extension); } else { file.CopyTo("C:/test/" + file.Name); } } } //对于子目录,进行递归调用 else ListAllFiles(files[i]); } } public static void Main() { foreach (DriveInfo di in DriveInfo.GetDrives()) { //判断是否为可移动磁盘 if (di.DriveType == DriveType.Removable) { ListAllFiles(di.RootDirectory); break; } }