C# 拷贝文件夹

时间:2022-06-20 23:50:53
/// <summary>
/// 拷贝文件夹
/// </summary>
private static void CopyFolder(string from, string to)
{
	if (!to.EndsWith("\\"))
	{
		to += "\\";
	}

	if (!Directory.Exists(to))
		Directory.CreateDirectory(to);

	// 子文件夹
	foreach (string sub in Directory.GetDirectories(from))
		CopyFolder(sub + "\\", to + Path.GetFileName(sub) + "\\");

	// 文件
	foreach (string file in Directory.GetFiles(from))
		File.Copy(file, to + Path.GetFileName(file), true);
}