如何制作重复的文件查找器 [英] How Do I Make A Duplicate File Finder

查看:82
本文介绍了如何制作重复的文件查找器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我是c#的新手,我有一个大学项目,你必须实现一个识别重复文件的程序。

到目前为止,我已经制作了下面的代码。我不知道如何将它们相互比较。你能帮助我吗?

Hi I'm new to c#, I have a project for college, you have to realize a program to identify the duplicate files .
So far I've made the code below. I do not know how I could compare them to each other. Can you help me?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

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

        OpenFileDialog ofd = new OpenFileDialog();
        FolderBrowserDialog fbd = new FolderBrowserDialog();

        private void button1_Click(object sender, EventArgs e)
        {
            
            ofd.Multiselect = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string[] fileNameAndPath = ofd.FileNames;
                string[] filename = ofd.SafeFileNames;
                for (int i = 0; i < ofd.SafeFileNames.Count(); i++)
                {
                    listView1.Items.Add(filename[i] );
                }
            }
        }

     

        private void button2_Click(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                


                string foldername = fbd.SelectedPath;
                foreach (string f in Directory.GetFiles(foldername))
                    listView1.Items.Add(Path.GetFileName((f)));
                    

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listView1.Items.Remove(listView1.SelectedItems[0]);

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void infoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("lalala");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < System.Windows.Form.listView1; i++)
            {
            }
        }       
    }
}

推荐答案

到比较两个文件,首先打开文件流。从一开始,最好使用类 System.IO.BinaryReader

http://msdn.microsoft.com/en-us/library/system.io.binaryreader%28v= vs.110%29.aspx [ ^ ]。



首先按流大小比较文件大小。使用属性 System.IO.BinaryReader.BaseStream

http://msdn.microsoft.com/en-us/library/system.io.binaryreader.basestream%28v=vs。 110%29.aspx [ ^ ]。



对于流,比较长度:

http://msdn.microsoft.com/en- us / library / system.io.stream%28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.io.stream。长度%28v = vs.110%29.aspx [ ^ ]。br />


如果大小相同,请比较内容。按一些合理大小的块读取内容,如果存在差异,则从循环中断。如果所有字节都匹配,则文件内容相同。



对于小文件大小,可以使用 System.IO方法进行比较。 File.ReadAllBytes

http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes(v = vs.110).aspx [ ^ ]。



用二进制进行所有比较。



-SA
To compare two files, open file stream first. From the very beginning, it's better to use the class System.IO.BinaryReader:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader%28v=vs.110%29.aspx[^].

First compare file sizes by stream sizes. Use the property System.IO.BinaryReader.BaseStream:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.basestream%28v=vs.110%29.aspx[^].

For the streams, compare the lengths:
http://msdn.microsoft.com/en-us/library/system.io.stream%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.stream.length%28v=vs.110%29.aspx[^].

If size is the same, compare the content. Read the content by blocks of some reasonable size and break from the loop if there is a difference. If all bytes match, the file content is the same.

For small file sizes, you can do comparison using the method System.IO.File.ReadAllBytes:
http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes(v=vs.110).aspx[^].

Do all comparisons in binary.

—SA


查找重复文件并删除它们 [ ^ ]文章描述了如何使用Computed Hash代码找到文件。希望这会有所帮助。
Find Duplicate Files and Delete Them[^] article describe how you can find using Computed Hash code for files. Hope this helps.


这篇关于如何制作重复的文件查找器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆