欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

c#深拷贝文件夹示例

时间:2020-11-10 15:45:12|栏目:.NET代码|点击:

复制代码 代码如下:

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

上一篇:在ASP.NET 2.0中操作数据之五十:为GridView控件添加Checkbox

栏    目:.NET代码

下一篇:C#中简单的拆箱操作用法实例分析

本文标题:c#深拷贝文件夹示例

本文地址:http://www.codeinn.net/misctech/21024.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有