Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12437636
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C#/.net

2014-08-22 15:27:00


  1. private void CopyDir(string srcPath, string aimPath)
  2.         {
  3.             try
  4.             {
  5.                 // 检查目标目录是否以目录分割字符结束如果不是则添加之
  6.                 if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
  7.                 {
  8.                     aimPath += System.IO.Path.DirectorySeparatorChar;
  9.                 }

  10.                 // 判断目标目录是否存在如果不存在则新建之
  11.                 if (!System.IO.Directory.Exists(aimPath))
  12.                 {
  13.                     System.IO.Directory.CreateDirectory(aimPath);
  14.                 }

  15.                 // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
  16.                 // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
  17.                 // string[] fileList = Directory.GetFiles(srcPath);
  18.                 string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);

  19.                 // 遍历所有的文件和目录
  20.                 foreach (string file in fileList)
  21.                 {
  22.                     // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
  23.                     if (System.IO.Directory.Exists(file))
  24.                     {
  25.                         CopyDir(file, aimPath + System.IO.Path.GetFileName(file));
  26.                     }

  27.                     // 否则直接Copy文件
  28.                     else
  29.                     {
  30.                         System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file), true);
  31.                     }
  32.                 }
  33.             }
  34.             catch (Exception e)
  35.             {
  36.                 throw;
  37.             }
  38.         }


转自博客:
http://blog.csdn.net/flysun0311/article/details/4108088
阅读(2010) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~