手机
当前位置:查字典教程网 >编程开发 >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#身份证验证小例子

C# 对文件与文件夹的操作包括删除、移动与复制

c# 文件(夹)创建与删除

c#(Socket)同步套接字代码示例

c#使用linq技术创建xml文件的小例子

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

用c#获得当前用户的Application Data文件夹位置

C#获取全部目录和文件的简单实例

C#加密解密文件小工具实现代码

精品推荐
分类导航