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的實現(xiàn)方法
C#調(diào)用C++的非托管類的dll其實很簡單基本就是固定的調(diào)用格式,有需要的朋友可以參考一下2013-10-10
C#抓取網(wǎng)頁數(shù)據(jù) 解析標(biāo)題描述圖片等信息 去除HTML標(biāo)簽
本文主要一步一步介紹利用C#抓取頁面數(shù)據(jù)的過程,抓取HTML,獲取標(biāo)題、描述、圖片等信息,并去除HTML,希望對大家有所幫助。2016-04-04
WinForm實現(xiàn)程序一段時間不運行自動關(guān)閉的方法
這篇文章主要介紹了WinForm實現(xiàn)程序一段時間不運行自動關(guān)閉的方法,涉及WinForm計時器及進程操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
C#實現(xiàn)把圖片轉(zhuǎn)換成二進制以及把二進制轉(zhuǎn)換成圖片的方法示例
這篇文章主要介紹了C#實現(xiàn)把圖片轉(zhuǎn)換成二進制以及把二進制轉(zhuǎn)換成圖片的方法,結(jié)合具體實例形式分析了基于C#的圖片與二進制相互轉(zhuǎn)換以及圖片保存到數(shù)據(jù)庫的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
C#實現(xiàn)GridView導(dǎo)出Excel實例代碼
本篇文章主要介紹了C#實現(xiàn)GridView導(dǎo)出Excel實例代碼,這里整理了詳細(xì)的代碼,非常具有實用價值,需要的朋友可以參考下。2017-03-03

