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

需要引用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;
}

使用方法:

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

上面的方法将把c:\temp\index目录下的所有子目录和文件复制到 c:\temp\newindex目录下。

最新文章

  1. MyEclipse修改项目名称后,部署到tomcat问题
  2. 了解Sql Server的执行计划
  3. .net接口学习笔记
  4. mysql高可用之LVS + KEEPALIVE + MYSQL
  5. (转载)mysql decimal、numeric数据类型
  6. C++静态成员函数不能调用非静态成员变量
  7. js timer
  8. WebBench的安装与使用
  9. RabbitMQ+Spring 结合使用
  10. 『宝藏 状态压缩DP NOIP2017』
  11. 在Linux系统中同步更新我们的Github博客
  12. 51Nod1123 X^A Mod B 数论 中国剩余定理 原根 BSGS
  13. Zabbix-2--安装--LAMP/LNMP详细总结
  14. Windows Server 2012 RS 配置IIS8.0+发布网站
  15. Hadoop之mapreduce
  16. Windows XP Professional产品序列号
  17. Gitlab+Jenkins学习之路(十四)之自动化脚本部署实践
  18. Anaconda完全入门指南
  19. golang之log rotate
  20. Java从零开始学九(数组)

热门文章

  1. HttpUrlConnection访问Servlet进行数据传输
  2. Centos添加新硬盘、分区、格式化、自动挂载
  3. maven 创建Hadoop程序
  4. JS学习之DOM节点的关系属性封装、克隆节点、Dom中Style常用的一些属性等小结
  5. node应用场景
  6. 弹出JS提示框
  7. 关闭Ubuntu 12.04的内部错误提示
  8. getUserMedia
  9. Makecert.exe(证书创建工具)
  10. 设计模式之美:Abstract Factory(抽象工厂)