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)
        {

        }
    }
}

下载

源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱蜂朝王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值