// 删除文件或者目录 // parameters: // root_paths 可用的根目录列表。只要在其中之一以下,就允许删除。否则不允许删除 // strCurrentDirectory 当前路径,注意这是物理路径 // return: // -1 出错 // 其他 实际删除的文件和目录个数 public int DeleteFile( // string strRootPath, List <string> root_paths, string strCurrentDirectory, string strPattern, out string strError) { strError = ""; List <string> errors = new List <string>(); try { FileSystemLoader loader = new FileSystemLoader(strCurrentDirectory, strPattern); loader.ListStyle = ListStyle.None; int count = 0; foreach (FileSystemInfo si in loader) { // 检查文件或目录必须在根以下。防止漏洞 //if (PathUtil.IsChildOrEqual(si.FullName, strRootPath) == false) // continue; if (IsChildOrEqual(si.FullName, root_paths) == false) { errors.Add("文件 " + si.Name + " 越过了限制目录,删除操作被拒绝"); continue; } if (si is DirectoryInfo) { _physicalFileCache.ClearAll(); PathUtil.DeleteDirectory(si.FullName); } if (si is FileInfo) { // File.Delete(si.FullName); _physicalFileCache.FileDelete(si.FullName); // 2017/9/24 // 顺带删除同名的 .~state 文件 string strTempFileName = si.FullName + ".~state"; if (File.Exists(strTempFileName)) { File.Delete(strTempFileName); count++; } } count++; } if (errors.Count > 0) { strError = StringUtil.MakePathList(errors, "; "); } return(count); } catch (DirectoryNotFoundException) { return(0); } catch (Exception ex) { strError = ExceptionUtil.GetAutoText(ex); return(-1); } }