/// <summary> /// 删除过早的临时文件 /// </summary> /// <param name="path">包含临时文件的目录,删除时会查找所有子目录</param> /// <param name="timeAgo">指定一个时间间隔,超过这个时间间隔前的文件将被删除</param> /// <param name="topDirectoryOnly">是否只清除指定目录,不包含它的子目录</param> /// <returns>过程中所有遇到的异常</returns> public static List <Exception> DeleteOldFiles(string path, TimeSpan timeAgo, bool topDirectoryOnly = true) { List <Exception> list = new List <Exception>(); try { if (Directory.Exists(path) == false) { return(list); } SearchOption searchOption = topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories; IEnumerable <string> files = Directory.EnumerateFiles(path, "*.*", searchOption); DateTime now = DateTime.Now; foreach (string file in files) { // 清除过程中,也有可能其它进程正在删除文件,所有文件不存在就忽略 if (RetryFile.Exists(file) == false) { continue; } // 以文件的最后修改时间做为对比标准 DateTime time = RetryFile.GetLastWriteTime(file); TimeSpan span = now - time; // 删除 指定时间 前的文件 if (span >= timeAgo) { try { //Console.WriteLine(file); RetryFile.Delete(file); } catch (Exception ex) { list.Add(ex); } } } } catch (Exception ex2) { list.Add(ex2); } return(list); }
/// <summary> /// 删除临时文件 /// </summary> /// <param name="path">要执行删除的根目录</param> /// <param name="timeAgo">一个时间间隔,表示需要删除多久前的文件</param> /// <param name="topDirectoryOnly">是否只扫描指定的根目录(不包含子目录),如果需要扫描子目录,请指定为 false</param> public void DeleteFiles(string path, TimeSpan timeAgo, bool topDirectoryOnly) { this.Count = 0; this.Exceptions.Clear(); if (string.IsNullOrEmpty(path) || Directory.Exists(path) == false) { return; } DateTime now = DateTime.Now; SearchOption searchOption = topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories; try { IEnumerable <string> files = Directory.EnumerateFiles(path, "*.*", searchOption); foreach (string file in files) { // 清除过程中,也有可能其它进程正在删除文件,所有文件不存在就忽略 if (RetryFile.Exists(file) == false) { continue; } // 以文件的最后修改时间做为对比标准 DateTime time = RetryFile.GetLastWriteTime(file); TimeSpan span = now - time; // 删除 指定时间 前的文件 if (span >= timeAgo) { try { RetryFile.Delete(file); this.Count++; } catch (Exception ex) { this.Exceptions.Add(ex); } } } } catch (Exception ex2) { this.Exceptions.Add(ex2); } }
private static string HashFile(HashAlgorithm hash, string filePath) { if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException(nameof(filePath)); } if (RetryFile.Exists(filePath) == false) { throw new FileNotFoundException("文件不存在:" + filePath); } using (FileStream fs = RetryFile.OpenRead(filePath)) { byte[] buffer = hash.ComputeHash(fs); return(buffer.ToHexString()); } }
/// <summary> /// 快速计算文件哈希值(只计算文件开头部分) /// </summary> /// <param name="filePath">要计算哈希值的文件路径</param> /// <param name="headerLength">读取开头多长的部分,默认值:2M</param> /// <returns></returns> public static string FastHash(string filePath, int headerLength = 2 * 1024 * 1024) { if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException(nameof(filePath)); } if (RetryFile.Exists(filePath) == false) { throw new FileNotFoundException("文件不存在:" + filePath); } if (headerLength <= 0) { throw new ArgumentOutOfRangeException(nameof(headerLength)); } using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) { using (FileStream file = RetryFile.OpenRead(filePath)) { if (headerLength > file.Length) { headerLength = (int)file.Length; } // 将文件长度转成字节数组 byte[] intBytes = BitConverter.GetBytes(file.Length); if (BitConverter.IsLittleEndian) { Array.Reverse(intBytes); } // 申请字节缓冲区 byte[] buffer = new byte[headerLength + intBytes.Length]; Array.Copy(intBytes, buffer, intBytes.Length); // 读取文件的开头部分 file.Read(buffer, intBytes.Length, headerLength); // 计算缓冲区的哈希值 byte[] result = md5.ComputeHash(buffer); return(BitConverter.ToString(result).Replace("-", "")); } } }