C# 图标标注小工具-查看重复文件

目录

效果

项目

代码

下载


效果

项目

代码

复制代码
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace ImageDuplicate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string FolderPath = "";
        List<ImgFileInfo> ltFile = new List<ImgFileInfo>();

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();

            if (dialog.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            textBox1.Text = "";
            FolderPath = "";
            ltFile.Clear();
            dataGridView1.DataSource = null;

            Application.DoEvents();

            button1.Enabled = false;
            button2.Enabled = false;

            FolderPath = dialog.SelectedPath + @"\";
            textBox1.Text = FolderPath;

            DirectoryInfo root = new DirectoryInfo(FolderPath);

            foreach (FileInfo f in root.GetFiles())
            {
                ltFile.Add(new ImgFileInfo(f.Name, GetFileMD5(FolderPath + f.Name)));
            }

            //var sortlist = ltFile.OrderBy(o => o.md5).ToList();

            var listGroup = ltFile.GroupBy(o => o.md5)
                .Select(g => (new
                {
                    md5 = g.Key,
                    num = g.Count(),
                    fileName = string.Join(",", g.Select(d => d.fileName))
                }))
                .OrderByDescending(o => o.num)
                .ThenBy(o => o.md5)
                .ToList();

            dataGridView1.DataSource = listGroup;

            //自适应所有列的宽度
            //dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

            dataGridView1.Columns[0].Width = 300;
            dataGridView1.Columns[1].Width = 50;
            dataGridView1.Columns[2].Width = 850;

            button1.Enabled = true;
            button2.Enabled = true;

        }

        public static string GetFileMD5(string path)
        {
            if (!System.IO.File.Exists(path)) return "";
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider();
            byte[] buffer = md5Provider.ComputeHash(fs);
            string resule = BitConverter.ToString(buffer);
            md5Provider.Clear();
            fs.Close();
            return resule;
        }

        /// <summary>
        /// 去重复
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}

下载

源码下载

相关推荐
大邳草民3 分钟前
深入理解 Python 的“左闭右开”设计哲学
开发语言·笔记·python
实心儿儿4 分钟前
C++ —— list
开发语言·c++
Never_Satisfied7 分钟前
在JavaScript中,将包含HTML实体字符的字符串转换为普通字符
开发语言·javascript·html
开开心心就好24 分钟前
电脑音质提升:杜比全景声安装详细教程
java·开发语言·前端·数据库·电脑·ruby·1024程序员节
t1987512830 分钟前
基于多假设跟踪(MHT)算法的MATLAB实现
开发语言·matlab
跟着珅聪学java33 分钟前
在Java中判断Word文档中是否包含表格并读取表格内容,可以使用Apache POI库教程
java·开发语言·word
我也要当昏君1 小时前
5.3 【2012统考真题】
开发语言·智能路由器·php
初见无风1 小时前
3.4 Boost库intrusive_ptr智能指针的使用
开发语言·boost
程序猿20231 小时前
Python每日一练---第六天:罗马数字转整数
开发语言·python·算法