博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

C# 拷贝文件夹

Posted on 2012-09-07 17:57  PHP-张工  阅读(512)  评论(0编辑  收藏  举报
/// <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);
}