c#深拷贝文件夹示例_C#教程-查字典教程网
c#深拷贝文件夹示例
c#深拷贝文件夹示例
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.IO;us...

复制代码 代码如下:

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#教程学习
    热门C#教程学习
    编程开发子分类