/// <summary> /// 复制文件夹 /// </summary> /// <param name="SourcePath">需要被复制的路径</param> /// <param name="DestinationPath">复制到的文件夹</param> /// <param name="overwriteexisting">true:覆盖现有文件,false:不覆盖现有文件</param> /// <returns></returns> public List <string> CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting = true) { fail.Clear(); allfile.Clear(); FileArg arg = new FileArg(HowManyFiles(SourcePath).ToArray()); Copy(SourcePath, DestinationPath, overwriteexisting, arg); if (OnCopy != null) { arg.isok = true; OnCopy(this, arg); } return(fail); }
/// <summary> /// 删除指定文件夹中的所有内容 /// </summary> /// <param name="SourcePath">需要删除的文件夹路径</param> /// <param name="returnItem">所需返回的值 all:删除失败的文件夹和文件数 FailToDeleteFile:删除失败的文件 FailToDeleteFolder:删除失败的文件夹</param> /// <returns></returns> public int[] DeleteFolder(string SourcePath, ReturnItem returnItem = ReturnItem.All) { DelFailItem[0] = 0; DelFailItem[1] = 0; allfile.Clear(); int[] res = new int[1]; List <string> path = HowManyFiles(SourcePath); FileArg deletearg = new FileArg(path.ToArray()); for (int i = 0; i < path.Count; i++) { try { System.IO.File.Delete(path[i]); if (OnDelete != null) { deletearg.hasDone++; deletearg.nowPath = path[i]; OnDelete(this, deletearg); } } catch { DelFailItem[0]++; } } if (OnDelete != null) { deletearg.isok = true; OnDelete(this, deletearg); } DeleteEmptyFolder(SourcePath, true); switch (returnItem) { case ReturnItem.All: return(DelFailItem); case ReturnItem.FailToDeleteFile: res[0] = DelFailItem[0]; return(res); case ReturnItem.FailToDeleteFolder: res[0] = DelFailItem[1]; return(res); default: return(DelFailItem); } }
private bool Copy(string SourcePath, string DestinationPath, bool overwriteexisting = true, FileArg arg = null) { 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); if (OnCopy != null) { arg.hasDone++; arg.nowPath = fls + "->" + DestinationPath + flinfo.Name; OnCopy(this, arg); } } foreach (string drs in Directory.GetDirectories(SourcePath)) { DirectoryInfo drinfo = new DirectoryInfo(drs); if (Copy(drs, DestinationPath + drinfo.Name, overwriteexisting, arg) == false) { fail.Add(drs); ret = false; } } } ret = true; } catch { ret = false; } return(ret); }