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# 調(diào)用C++寫的dll的實(shí)現(xiàn)方法
C#調(diào)用C++的非托管類的dll其實(shí)很簡(jiǎn)單基本就是固定的調(diào)用格式,有需要的朋友可以參考一下2013-10-10
C#抓取網(wǎng)頁(yè)數(shù)據(jù) 解析標(biāo)題描述圖片等信息 去除HTML標(biāo)簽
本文主要一步一步介紹利用C#抓取頁(yè)面數(shù)據(jù)的過程,抓取HTML,獲取標(biāo)題、描述、圖片等信息,并去除HTML,希望對(duì)大家有所幫助。2016-04-04
WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法,涉及WinForm計(jì)時(shí)器及進(jìn)程操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
C#實(shí)現(xiàn)的SN快速輸入工具實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的SN快速輸入工具,以實(shí)例的形式詳細(xì)講述了C#實(shí)現(xiàn)序列號(hào)快速輸入的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11
C#實(shí)現(xiàn)把圖片轉(zhuǎn)換成二進(jìn)制以及把二進(jìn)制轉(zhuǎn)換成圖片的方法示例
這篇文章主要介紹了C#實(shí)現(xiàn)把圖片轉(zhuǎn)換成二進(jìn)制以及把二進(jìn)制轉(zhuǎn)換成圖片的方法,結(jié)合具體實(shí)例形式分析了基于C#的圖片與二進(jìn)制相互轉(zhuǎn)換以及圖片保存到數(shù)據(jù)庫(kù)的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
C#中的高效IO庫(kù)System.IO.Pipelines
這篇文章介紹了C#中的高效IO庫(kù)System.IO.Pipelines,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
C#身份證識(shí)別相關(guān)技術(shù)功能詳解
這篇文章主要介紹了C#身份證識(shí)別相關(guān)技術(shù)詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
C#實(shí)現(xiàn)GridView導(dǎo)出Excel實(shí)例代碼
本篇文章主要介紹了C#實(shí)現(xiàn)GridView導(dǎo)出Excel實(shí)例代碼,這里整理了詳細(xì)的代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03

