日期:2014-05-18 浏览次数:20902 次
using System; using System.Collections.Generic; using System.IO; using System.Text; namespace ConsoleApplication1 { public class Program { /// <summary> /// 获取文件MD5 /// </summary> /// <param name="path">文件路径</param> /// <returns>MD5值</returns> public static String GetFileMD5(String path) { String strMD5 = String.Empty; try { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider()) { byte[] bytes = md5.ComputeHash(fs); strMD5 = System.BitConverter.ToString(bytes); } } } catch(Exception ex) { Console.WriteLine(String.Format("{0}\r\n{1}", path, ex.Message)); } return strMD5; } /// <summary> /// 删除重复文件 /// </summary> /// <param name="files">文件路径数组</param> private static void DeleteFile(String[] files) { List<String> MD5List = new List<String>(1000); String strMD5 = String.Empty; foreach (String path in files) { strMD5 = GetFileMD5(path); if (String.IsNullOrEmpty(strMD5)) { continue; } if (MD5List.Contains(strMD5)) { // 删除文件已存在 if (File.Exists(path)) { try { File.Delete(path); } catch(Exception ex) { Console.WriteLine(String.Format("{0}\r\n{1}", path, ex.Message)); } } } else { // 文件不存在,MD5加到列表 MD5List.Add(strMD5); } } } static void Main() { String[] files = Directory.GetFiles(@"c:\test"); if (files.Length > 1) { DeleteFile(files); } Console.ReadKey(); } } }
------解决方案--------------------