C#递归拷贝文件删除文件
2021-06-13 19:07
标签:rect reac string nbsp path not highlight 递归 copy 拷贝文件及子文件,最后一个参数排除,哪个不要删除。(其实就是移动的效果) 递归删除文件 C#递归拷贝文件删除文件 标签:rect reac string nbsp path not highlight 递归 copy 原文地址:https://www.cnblogs.com/sunliyuan/p/10450135.html //拷贝文件及子文件
public static void CopyDirectory(string src, string dest, string [] skips)
{
if (string.IsNullOrEmpty(src)) return;
CreateDirectoryIfNotExists(dest);
//拷贝子文件
foreach (string file in Directory.GetFiles(src))
{
string fileName = Path.GetFileName(file);
bool bSkip = false;
if (skips != null)
{
foreach (string skip in skips)
{
if (fileName.Contains(skip))
{
bSkip = true;
break;
}
}
}
if (bSkip) continue;
try
{
System.IO.File.Copy(file, dest + "\\" + fileName, true);
}catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
//递归拷贝
foreach (string dir in Directory.GetDirectories(src))
{
string[] parts = dir.Split(‘\\‘);
string lastDirPart = parts[parts.Length - 1];
CopyDirectory(dir, dest + "\\" + lastDirPart, skips);
}
}
public static void DeleteDirectory(string src, string[] skips)
{
if (string.IsNullOrEmpty(src)) return;
if (src.Length