拷贝目录及目录下的文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class DirCopy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
#region 拷贝目录及目录下的文件
/// <summary>
/// 拷贝目录及目录下的文件
/// </summary>
/// <param name="oldDir">原目录</param>
/// <param name="newDir">目标目录</param>
private void CopyDirectory(DirectoryInfo oldDir, DirectoryInfo newDir)
{
try
{
if (!oldDir.Exists)
{
Response.Write("目录不存在!");
Response.End();
return;
}
if (!newDir.Exists)
{
newDir.Create();
}
foreach (FileInfo fi in oldDir.GetFiles())
{
fi.CopyTo(newDir.FullName + @"\" + fi.Name, true);
}
foreach (DirectoryInfo dr in oldDir.GetDirectories())
{
CopyDirectory(dr, new DirectoryInfo(newDir.FullName + @"\" + dr.Name));
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
#endregion
protected void Button1_Click(object sender, EventArgs e)
{
DirectoryInfo oldDir = new DirectoryInfo(Server.MapPath("./aa"));
DirectoryInfo newDir = new DirectoryInfo(Server.MapPath("./App_Data"));
newDir.Delete(true);
CopyDirectory(oldDir, newDir);
}
}
lei1217