zoukankan      html  css  js  c++  java
  • 【算法系列】使用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();  
            }  
        }  

    相当精妙地写法:)

    见老外的文章:http://www.hosca.com/blog/post/2010/04/13/using-LINQ-to-detect-and-remove-duplicate-files.aspx

  • 相关阅读:
    web移动端开发经验总结
    《前端JavaScript面试技巧》笔记一
    《SEO在网页制作中的应用》视频笔记
    web前端开发笔记(2)
    OAuth2.0理解和用法
    基于hdfs文件创建hive表
    kafka 配置事项
    centos7时间同步
    lambda架构
    hbase hadoop版本
  • 原文地址:https://www.cnblogs.com/liping13599168/p/1781836.html
Copyright © 2011-2022 走看看