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();
        }
    }
}

标签:c#,文件夹
0
投稿

猜你喜欢

  • MyBatis框架之mybatis逆向工程自动生成代码

    2023-01-11 06:19:18
  • 轻松掌握Java观察者模式

    2023-10-10 22:34:54
  • 值得Java开发者关注的7款新工具

    2023-11-02 23:05:31
  • java实现excel和txt文件互转

    2023-10-07 23:04:05
  • Linux下java环境配置图文方法

    2021-10-24 06:22:52
  • C#多线程之线程池ThreadPool详解

    2021-10-30 23:59:45
  • java 将一个数组逆序输出的方法

    2023-05-09 09:29:01
  • Android 双击Back键退出应用的实现方法

    2023-07-06 05:41:41
  • Java简单实现定时器

    2023-07-16 18:10:58
  • C#使用Task实现异步方法

    2022-09-02 20:26:53
  • Android.bp语法和使用方法讲解

    2022-09-29 19:31:19
  • 简述Java编程之关系操作符

    2021-08-07 09:44:23
  • Android API开发之SMS短信服务处理和获取联系人的方法

    2021-10-23 03:22:15
  • 使用javaMail实现发送邮件

    2023-01-20 21:22:26
  • Spark SQL关于性能调优选项详解

    2021-11-05 22:32:56
  • GraphQL入门总体创建教程

    2022-10-16 12:29:09
  • java 中同步、异步、阻塞和非阻塞区别详解

    2023-06-21 19:26:07
  • MyBatis数据脱敏的实现方案介绍

    2021-10-06 19:22:34
  • Spring注解之@Lazy注解使用解析

    2023-08-28 23:12:23
  • Logger.getLogger()与LogFactory.getLog()的区别详解

    2021-11-04 19:16:00
  • asp之家 软件编程 m.aspxhome.com