c#深拷贝文件夹示例
时间:2023-07-24 07:50:40
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace FileUtility
{
public class Program
{
public static void DeepCopy(DirectoryInfo source, DirectoryInfo target, params string[] excludePatterns)
{
if (target.FullName.Contains(source.FullName))
return;
// Go through the Directories and recursively call the DeepCopy Method for each one
foreach (DirectoryInfo dir in source.GetDirectories())
{
var dirName = dir.Name;
var shouldExclude = excludePatterns.Aggregate(false, (current, pattern) => current || Regex.Match(dirName, pattern).Success);
if (!shouldExclude)
DeepCopy(dir, target.CreateSubdirectory(dir.Name), excludePatterns);
}
// Go ahead and copy each file to the target directory
foreach (FileInfo file in source.GetFiles())
{
var fileName = file.Name;
var shouldExclude = excludePatterns.Aggregate(false,
(current, pattern) =>
current || Regex.Match(fileName, pattern).Success);
if (!shouldExclude)
file.CopyTo(Path.Combine(target.FullName, fileName));
}
}
static void Main(string[] args)
{
DeepCopy(new DirectoryInfo(@"d:/test/b"), new DirectoryInfo(@"d:/test/a"));
DeepCopy(new DirectoryInfo(@"d:/test/c"), new DirectoryInfo(@"d:/test/c/c.1"));
DeepCopy(new DirectoryInfo(@"d:/test/1/"), new DirectoryInfo(@"d:/test/2/"), new string[] { ".*\\.txt" });
Console.WriteLine("复制成功...");
Console.ReadKey();
}
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatis框架之mybatis逆向工程自动生成代码
![](https://img.aspxhome.com/file/2023/8/88578_0s.jpg)
轻松掌握Java观察者模式
值得Java开发者关注的7款新工具
![](https://img.aspxhome.com/file/2023/2/58862_0s.jpg)
java实现excel和txt文件互转
Linux下java环境配置图文方法
![](https://img.aspxhome.com/file/2023/0/70300_0s.png)
C#多线程之线程池ThreadPool详解
java 将一个数组逆序输出的方法
Android 双击Back键退出应用的实现方法
Java简单实现定时器
![](https://img.aspxhome.com/file/2023/6/76536_0s.jpg)
C#使用Task实现异步方法
![](https://img.aspxhome.com/file/2023/6/70436_0s.jpg)
Android.bp语法和使用方法讲解
![](https://img.aspxhome.com/file/2023/7/111717_0s.png)
简述Java编程之关系操作符
Android API开发之SMS短信服务处理和获取联系人的方法
使用javaMail实现发送邮件
![](https://img.aspxhome.com/file/2023/1/64121_0s.jpg)
Spark SQL关于性能调优选项详解
GraphQL入门总体创建教程
![](https://img.aspxhome.com/file/2023/3/117683_0s.png)
java 中同步、异步、阻塞和非阻塞区别详解
MyBatis数据脱敏的实现方案介绍
![](https://img.aspxhome.com/file/2023/2/68742_0s.png)