c#没有复制目录的代码,需要通过递归实现复制目录:

使用方法:

1、把c:\temp\index目录下的所有子目录和文件复制到 c:\temp\newindex目录下。

bool copy = CopyDirectory("c:\\temp\\index\\", "c:\\temp\\newindex\\", true);

2、具体代码实现

需要引用System.IO命名空间,实现代码如下:

private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
{
bool ret = false;
try
{
SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\"; if (Directory.Exists(SourcePath))
{
if (Directory.Exists(DestinationPath) == false)
Directory.CreateDirectory(DestinationPath); foreach (string fls in Directory.GetFiles(SourcePath))
{
FileInfo flinfo = new FileInfo(fls);
flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
}
foreach (string drs in Directory.GetDirectories(SourcePath))
{
DirectoryInfo drinfo = new DirectoryInfo(drs);
if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
ret = false;
}
}
ret = true;
}
catch (Exception ex)
{
ret = false;
}
return ret;
}
3、直接把代码拷贝到你的程序就大功告成 以上仅为记录,转载来源为:https://www.cnblogs.com/meetrice/p/5397289.html

最新文章

  1. js 字符串格式化方法
  2. Linux常用目录
  3. 第 29 章 CSS3 弹性伸缩布局[中]
  4. 定制Eclipse IDE之功能篇(二)
  5. oracle 字符乱码问题解决方案
  6. Java线程专栏文章汇总(转)
  7. 淘宝的ruby镜像已无人维护,使用ruby-china的RubyGems镜像
  8. [翻译][MVC 5 + EF 6] 8:更新相关数据
  9. MapReduce详解
  10. 给定一个无序数组arr,求出需要排序的最短子数组长度。例如: arr = [1,5,3,4,2,6,7] 返回4,因为只有[5,3,4,2]需要排序。
  11. android开发之调试技巧
  12. 干货!分享一款windows下的磁盘分析神器。
  13. Java中的String,StringBuilder,StringBuffer
  14. python全栈开发day102-django rest-framework框架
  15. centos7 操作防火墙
  16. 函数和常用模块【day06】:re模块(九)
  17. 在Postgresql中添加新角色(Role)
  18. postman 官方 test 脚本样例
  19. IBM MR10i阵列卡配置Raid0/Raid1/Raid5(转)
  20. sshd_config优化

热门文章

  1. P1165 日志分析
  2. 两个input可能会用到的小方法
  3. Codewars练习Python
  4. Greenplum开发
  5. github与git常用的一些基本配置与命令
  6. (转)Hibernate框架基础——Java对象持久化概述
  7. POJ_1083_(思维)
  8. CAD得到所有实体2
  9. Redis系列(十)--集群cluster
  10. 新安装数据库sqlserver2008r2,使用javaweb连接不上问题处理