手机
当前位置:查字典教程网 >编程开发 >C#教程 >c#深拷贝文件夹示例
c#深拷贝文件夹示例
摘要:复制代码代码如下: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# 文件(夹)创建与删除

c# 调用.bat文件的实现代码

c#文件的复制,移动,创建(实例代码)

C# 执行bat批处理文件的小例子

C# 泛型深入理解介绍

c#图片添加水印的实例代码

C# 汉字转拼音(全拼和首字母)实例

计算字符串和文件MD5值的小例子

c#读取xml文件到datagridview实例

c# 删除所有的空文件夹的小例子

精品推荐
分类导航