• 技术文章 >后端开发 >C#.Net教程

    c#深拷贝文件夹示例

    小葫芦小葫芦2017-01-19 13:33:25原创498
    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#深拷贝文件夹示例相关文章请关注PHP中文网!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:c# 文件夹
    上一篇:C#运用FileInfo类实现拷贝文件的方法 下一篇:C# 拷贝数组的几种方法
    大前端线上培训班

    相关文章推荐

    • c++数组怎么初始化• 数组指针和指针数组的区别是什么• C++类型如何进行转换• C++中内存泄漏的检测• C# 2.0 Specification (泛型三)

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网