文件夹复制
Posted wgj-blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件夹复制相关的知识,希望对你有一定的参考价值。
/// <summary>
/// 复制文件夹
/// </summary>
/// <param name="SourcePath">源路径</param>
/// <param name="DestinationPath">目标路径</param>
/// <param name="overwriteexisting">是否覆盖现有</param>
/// <returns></returns>
private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
{
bool ret = false;
try
{
SourcePath = SourcePath.EndsWith(@"") ? SourcePath : SourcePath + @"";
DestinationPath = DestinationPath.EndsWith(@"") ? DestinationPath : DestinationPath + @"";
if (Directory.Exists(SourcePath))
{
if (Directory.Exists(DestinationPath) == false)
Directory.CreateDirectory(DestinationPath);
foreach (string fls in Directory.GetFiles(SourcePath))
{
FileInfo flinfo = new FileInfo(fls);
flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
}
foreach (string drs in Directory.GetDirectories(SourcePath))
{
DirectoryInfo drinfo = new DirectoryInfo(drs);
if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
ret = false;
}
}
ret = true;
}
catch (Exception ex)
{
ret = false;
}
return ret;
}
以上是关于文件夹复制的主要内容,如果未能解决你的问题,请参考以下文章