【算法系列】使用LINQ来检测和删除重复的文件
代码
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace DupeFinder
{
internal class Program
{
private static void Main(string[] args)
{
Directory.GetFiles(@"d:\icons", "*.ico")
.Select(
f => new
{
FileName = f,
FileHash = Encoding.UTF8.GetString( new SHA1Managed()
.ComputeHash(new FileStream(f, FileAccess.Read)))
})
.GroupBy(f => f.FileHash)
.Select(g => new {FileHash = g.Key, Files = g.Select(z => z.FileName).ToList()})
.SelectMany(f => f.Files.Skip(1))
.ToList()
.ForEach(File.Delete);
Console.ReadKey();
}
}
}
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace DupeFinder
{
internal class Program
{
private static void Main(string[] args)
{
Directory.GetFiles(@"d:\icons", "*.ico")
.Select(
f => new
{
FileName = f,
FileHash = Encoding.UTF8.GetString( new SHA1Managed()
.ComputeHash(new FileStream(f, FileAccess.Read)))
})
.GroupBy(f => f.FileHash)
.Select(g => new {FileHash = g.Key, Files = g.Select(z => z.FileName).ToList()})
.SelectMany(f => f.Files.Skip(1))
.ToList()
.ForEach(File.Delete);
Console.ReadKey();
}
}
}
相当精妙地写法:)
见老外的文章:http://www.hosca.com/blog/post/2010/04/13/using-LINQ-to-detect-and-remove-duplicate-files.aspx