zoukankan      html  css  js  c++  java
  • C# 中利用 CRC32 值判断文件是否重复

    需要在 NuGet 中引用 Crc32.NET 包

    直接贴代码了:

    using Force.Crc32;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Crc32NETDemo.ConApp.Codes;
    
    namespace Crc32NETDemo.ConApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                string folderPath = @"D:music";
                Console.WriteLine(string.Format("正在分析路径 {0} 下的文件的 CRC32 值。", folderPath));
                IEnumerable<string> filePathList = Directory.EnumerateFiles(folderPath, "*", SearchOption.AllDirectories);
                Dictionary<string, uint> dic = new Dictionary<string, uint>();
                foreach (string filePath in filePathList)
                {
                    byte[] zipdata = File.ReadAllBytes(filePath);
                    uint crc = Crc32CAlgorithm.Compute(zipdata);
                    dic.Add(filePath, crc);
                }
    
                Console.WriteLine("重复的文件如下:" + Environment.NewLine);
                IEnumerable<uint> doubledCrc32List = dic.Values.Iterative();
                if (doubledCrc32List.Any())
                {
                    var tempList = dic.Where(c => doubledCrc32List.Contains(c.Value)).OrderBy(c => c.Value).ToList();
                    foreach (var item in tempList)
                    {
                        Console.WriteLine(string.Format("路径:{0}, CRC32 值:{1}", item.Key, item.Value));
                    }
                }
                else
                {
                    Console.WriteLine("没有找到重复的文件!");
                }
                Console.WriteLine();
                Console.ReadLine();
            }
        }
    }

    谢谢浏览!

  • 相关阅读:
    我还没死!!微信公众号——自媒体的营销之路
    网页中嵌入视频
    保存对象到文件中
    bash array
    正则表达式如何验证邮箱
    software testing
    Verification and validation
    bash array
    12 Linux Which Command, Whatis Command, Whereis Command Examples
    如何进行产品路标规划和项目排序?
  • 原文地址:https://www.cnblogs.com/Music/p/crc32-demo-in-dotnet.html
Copyright © 2011-2022 走看看