• 【算法系列】使用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

  • 相关阅读:
    ecshop后台新建一个模块,添加分页
    Mysql中设置远程访问的方法
    Thinkphp 3.2中文章详情页的上一篇 下一篇文章功能
    Thinkphp 3.2.2 利用phpexcel完成excel导出功能
    Thinkphp 3.2中字符串截取
    服务管理-文件服务器
    nginx-伤心的事
    shell-awk
    shell-sed
    shell-函数、数组、正则
  • 原文地址:https://www.cnblogs.com/liping13599168/p/1781836.html
Copyright © 2020-2023  润新知