标签:rect reac string nbsp path not highlight 递归 copy
拷贝文件及子文件,最后一个参数排除,哪个不要删除。(其实就是移动的效果)
//拷贝文件及子文件 public static void CopyDirectory(string src, string dest, string [] skips) { if (string.IsNullOrEmpty(src)) return; CreateDirectoryIfNotExists(dest); //拷贝子文件 foreach (string file in Directory.GetFiles(src)) { string fileName = Path.GetFileName(file); bool bSkip = false; if (skips != null) { foreach (string skip in skips) { if (fileName.Contains(skip)) { bSkip = true; break; } } } if (bSkip) continue; try { System.IO.File.Copy(file, dest + "\\" + fileName, true); }catch(Exception ex) { Console.WriteLine(ex.Message); } } //递归拷贝 foreach (string dir in Directory.GetDirectories(src)) { string[] parts = dir.Split(‘\\‘); string lastDirPart = parts[parts.Length - 1]; CopyDirectory(dir, dest + "\\" + lastDirPart, skips); } }
递归删除文件
public static void DeleteDirectory(string src, string[] skips) { if (string.IsNullOrEmpty(src)) return; if (src.Length <= 10) { throw new Exception("文件路径小,删除请注意"); } //删除子文件 foreach (string file in Directory.GetFiles(src)) { string fileName = Path.GetFileName(file); bool bSkip = false; if (skips != null) { foreach (string skip in skips) { if (fileName.Contains(skip)) { bSkip = true; break; } } } if (bSkip) continue; try { File.Delete(file); //File.SetAttributes(file, FileAttributes.Normal); } catch (Exception ex) { Console.WriteLine(ex.Message); } } //递归删除 foreach (string dir in Directory.GetDirectories(src)) { DeleteDirectory(dir, skips); } //删除自己 if (skips == null) { Directory.Delete(src); } }
标签:rect reac string nbsp path not highlight 递归 copy
原文地址:https://www.cnblogs.com/sunliyuan/p/10450135.html