c#深拷貝文件夾示例
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("復(fù)制成功...");
Console.ReadKey();
}
}
}
相關(guān)文章
C# 中將數(shù)值型數(shù)據(jù)轉(zhuǎn)換為字節(jié)數(shù)組的方法
C# 中將數(shù)值型數(shù)據(jù)轉(zhuǎn)換為字節(jié)數(shù)組的方法,需要的朋友可以參考一下2013-05-05gridview的buttonfield獲取該行的索引值(實(shí)例講解)
本篇文章主要介紹了gridview的buttonfield獲取該行的索引值(實(shí)例講解)需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01C#中如何使用Winform實(shí)現(xiàn)炫酷的透明動(dòng)畫界面
這篇文章講解了如何使用Winform實(shí)現(xiàn)炫酷的透明動(dòng)畫界面,Winform相對于Wpf使用更簡單一些,系統(tǒng)要求更低,需要了解的朋友可以參考下2015-07-07c# winform取消右上角關(guān)閉按鈕的實(shí)現(xiàn)方法
本文是對c#中winform取消右上角關(guān)閉按鈕的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下。希望對大家有所幫助2013-10-10C#.NET采用HTML模板發(fā)送電子郵件完整實(shí)例
這篇文章主要介紹了C#.NET采用HTML模板發(fā)送電子郵件的方法,主要包括了HTML模板、替換函數(shù)與郵件函數(shù)三部分,是非常實(shí)用的功能,需要的朋友可以參考下2014-09-09C#實(shí)現(xiàn)Dev Grid拖拽移動(dòng)行的方法
這篇文章主要介紹了C#實(shí)現(xiàn)Dev Grid拖拽移動(dòng)行的方法,可實(shí)現(xiàn)Dev Grid拖拽移動(dòng)行的效果,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05