复制文件夹

 1  private bool CopyFolderFromSrcToDest(string sourcePath, string targetPath)
 2         {
 3             bool isCopyFlag = false;
 4             try
 5             {
 6                 if (Directory.Exists(sourcePath))
 7                 {
 8                     Directory.CreateDirectory(targetPath);
 9 
10                     string[] directories = Directory.EnumerateDirectories(sourcePath).ToArray();
11 
12                     if (directories.Length > 0)
13                     {
14                         foreach (string d in directories)
15                         {
16                             CopyFolderFromSrcToDest(d, targetPath + d.Substring(d.LastIndexOf("\\")));
17                         }
18                     }
19 
20                     string[] files = Directory.EnumerateFiles(sourcePath).ToArray();
21                     if (files.Length > 0)
22                     {
23                         foreach (string s in files)
24                         {
25                             File.Copy(s, targetPath + s.Substring(s.LastIndexOf("\\")));
26                         }
27                     }
28                 }
29                 else
30                 {
31                     MessageBox.Show("复制的源文件目录:" + sourcePath + "不存在");
32                 }
33             }
34             catch (Exception ex)
35             {
36                 isCopyFlag = false;
37             }
38             return isCopyFlag;
39         }

 

posted @ 2013-07-20 08:45  Space Tian  阅读(219)  评论(0编辑  收藏  举报