下面是我定的文件夹拷贝代码,运行时有问题,哪位能帮我调试一下。using System.Windows.Forms;
using System;
using System.IO;namespace YD.IO
{
/// <summary>
/// 文件夹相关操作
/// </summary>
public class Directory
{
/// <summary>
/// 将 srcPath 目录下所有文件和子文件夹复制到 destPath
/// </summary>
/// <param name="destPath">目标目录</param>
/// <param name="srcPath">源目录</param>
/// <returns>成功返回真,否则返回假</returns>
private static bool RecursionCopy(string destPath, string srcPath)
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (destPath[destPath.Length-1] != Path.DirectorySeparatorChar)
destPath += Path.DirectorySeparatorChar;
// 判断目标目录是否存在如果不存在则新建之
if (!System.IO.Directory.Exists(destPath))
System.IO.Directory.CreateDirectory(destPath);
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
// 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
// 遍历所有的文件和目录
try
{
foreach (string file in fileList)
{
// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
if (System.IO.Directory.Exists(file))
YD.IO.Directory.RecursionCopy(file, destPath+Path.GetFileName(file));
// 否则直接Copy文件
else
File.Copy(file, destPath+Path.GetFileName(file), true);
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
return false;
}
return true;
} /// <summary>
/// 将 srcPath 目录下所有文件和子文件夹复制到 destPath 目录下
/// </summary>
/// <param name="destPath">目标目录</param>
/// <param name="srcPath">源目录</param>
/// <returns>成功返回真,否则返回假</returns>
public static bool Copy(string destPath, string srcPath)
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (destPath[destPath.Length-1] != Path.DirectorySeparatorChar)
destPath += Path.DirectorySeparatorChar;
//改变目标目录
destPath += Path.GetFileName(srcPath);
//开始递归拷贝
if (!YD.IO.Directory.RecursionCopy(destPath, srcPath))
{
return false;
}
else
{
return true;
}
}
}
}多谢

解决方案 »

  1.   

    建议详细说明所出现的问题参考:
    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=208539
      

  2.   

    foreach (string file in fileList)
    {
    // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
    if (System.IO.Directory.Exists(file))
    YD.IO.Directory.RecursionCopy( destPath+Path.GetFileName(file),file);
    // 否则直接Copy文件
    else
    File.Copy(file, destPath+Path.GetFileName(file), true);
    }你改一改這里就可以啦
    YD.IO.Directory.RecursionCopy( destPath+Path.GetFileName(file),file);