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