我查了一天,写了分别用 FileInfo和File的两种,不过大致是一样的
static bool CopyDir(string fromdic,string todic)
{
bool flag = false;
DirectoryInfo fromDic = new DirectoryInfo(fromdic);
try
{
if (!Directory .Exists (todic ))
{
Directory.CreateDirectory(todic );
}
foreach (FileInfo file in fromDic .GetFiles ())
{
file.CopyTo(todic +"\\"+file .Name ,true );//允许覆盖现有文件
}
foreach (DirectoryInfo subDir in fromDic .GetDirectories () )
{
CopyDir(subDir.FullName ,todic +"\\"+subDir .Name );
}
flag = true;
}
catch (Exception)
{
throw;
}
return flag;
}
另一个
static bool CopyDir(string fromdic,string todic)
{
bool flag = false;
if (!Directory .Exists (todic ))
{
Directory.CreateDirectory(todic );
}
//复制所有文件
foreach (string filename in Directory.GetFiles(fromdic ) )
{
File.Copy(filename,todic+"\\"+Path .GetFileName (filename ),true );
}
//复制所有子文件夹(递归)
foreach (string subdic in Directory .GetDirectories (fromdic ))
{
CopyDir(subdic ,todic +"\\"+Path .GetFileName (todic ));
}
flag = true;
return flag;
} |