C#递归拷贝文件删除文件

拷贝文件及子文件,最后一个参数排除,哪个不要删除。(其实就是移动的效果)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//拷贝文件及子文件
       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);
           }
       }

  递归删除文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
           }
       }

  

 

posted @   石shi  阅读(271)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示