拷贝文件夹的文件到另一个目录

时间:2022-06-14 21:35:05
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Chareree.Utils.Update
{

/*
* Copy Files from souceFolder To TargetFolder
* Create Date 2012-11-20
* Author By Chareree
*/
public class FileManager
{
/*
* ManageFile.CopyFile(@"E:\Demo\SourceGridWork\AutoUpdate\bin\Debug",@"C:\Users\Chenl\Desktop\新建文件夹");
*/



/// <summary>
/// 删除临时下载的文件
/// </summary>
public static void DeleteTemp(string temp_path)
{

if (!Directory.Exists(temp_path))
return;
Directory.Delete(temp_path,true);
}
/// <summary>
///
/// </summary>
/// <param name="soucePath"></param>
/// <param name="targetPath"></param>
public static void CopyFile(string soucePath, string targetPath)
{

if (!Directory.Exists(soucePath))
return;
if (!Directory.Exists(targetPath))
return;

DirectoryInfo root_dir = new DirectoryInfo(soucePath);
CopyFile(soucePath, targetPath, root_dir);
}
/// <summary>
/// 拷贝文件到路径下
/// </summary>
/// <param name="soucePath"></param>
/// <param name="targetPath"></param>
private static void CopyFile(string soucePath, string targetPath, DirectoryInfo root_dir)
{


//DirectoryInfo root_dir = new DirectoryInfo(soucePath);
FileInfo[] files = root_dir.GetFiles();
foreach(FileInfo file in files)
{
file.CopyTo(Path.Combine(targetPath,file.Name),true);
//Console.WriteLine(file.Name);
}
DirectoryInfo[] dirs = root_dir.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
if (!Directory.Exists(Path.Combine(targetPath, dir.Name)))
Directory.CreateDirectory(Path.Combine(targetPath, dir.Name));
CopyFile(dir.FullName,Path.Combine(targetPath,dir.Name), dir);
}


// Directory.CreateDirectory(soucePath);

}


}
}