///// <summary> ///// 获取文件(向下钻取所有目录) ///// </summary> ///// <param name="path">路径</param> ///// <param name="pattern">通配符</param> ///// <returns></returns> //public static List<string> GetAllFile(string path, string pattern = "*") //{ // List<string> result = null; // try // { // result = Directory.EnumerateFiles(path, pattern, SearchOption.TopDirectoryOnly).ToList(); // } // catch (Exception e) { } // return result; //} /// <summary> /// 获取目录下的所有文件 /// 防止遇到($文件夹报错无法获取目录的错误) /// </summary> /// <param name="path"></param> /// <param name="patterns"></param> /// <returns></returns> public static List <string> GetAllFile(string path, string[] patterns = null) { List <string> allpath = DirTool.GetAllPath(path); if (allpath == null) { allpath = new List <string>(); } allpath.Add(path); return(FileTool.GetAllFile(allpath, patterns)); }
/// <summary> /// 清理之前版本遗留文件及空文件夹 /// </summary> /// <returns></returns> void CleanFile() { UIPbStatus(0); #region 除下载的更新文件和版本文件 try { Directory.Delete(R.AppPath + @"Temp\Update\", true); File.Delete(R.VersionFile); } catch { } #endregion UIPbStatus(50); if (version.DoClean) { #region 除非当前版本文件 List <string> file = FileTool.GetAllFile(R.AppPath); if (!ListTool.IsNullOrEmpty(file)) { foreach (var f in file) { int c = version.FileList.Where(x => x.File == "\\" + f.Replace(R.AppPath, "")).Count(); if (c == 0) { try { File.Delete(f); } catch { } } } Thread.Sleep(R.cst.WAIT_TIME); } #endregion #region 除空文件夹 List <string> path = DirTool.GetAllPath(R.AppPath); if (!ListTool.IsNullOrEmpty(path)) { path = path.OrderByDescending(x => x).ToList(); foreach (var p in path) { if (Directory.GetFiles(p).Length == 0 && Directory.GetDirectories(p).Length == 0) { if (Directory.Exists(p)) { try { Directory.Delete(p); } catch { } } } Thread.Sleep(R.cst.WAIT_TIME); } } #endregion } UIPbStatus(100); Step = 5; }
/// <summary> /// 清理之前版本遗留文件及空文件夹 /// </summary> /// <returns></returns> void CleanFile() { #region 除非当前版本文件 List <string> file = FileTool.GetAllFile(AppDir); if (!ListTool.IsNullOrEmpty(file)) { foreach (var f in file) { int c = version.FileList.Where(x => x.File == "\\" + f.Replace(AppDir, "")).Count(); if (c == 0) { File.Delete(f); } } Thread.Sleep(WAIT_TIME); } #endregion #region 除空文件夹 List <string> path = DirTool.GetAllPath(AppDir); if (!ListTool.IsNullOrEmpty(path)) { path = path.OrderByDescending(x => x).ToList(); foreach (var p in path) { if (Directory.GetFiles(p).Length == 0 && Directory.GetDirectories(p).Length == 0) { if (Directory.Exists(p)) { Directory.Delete(p); } } Thread.Sleep(WAIT_TIME); } } #endregion }