Click here to Skip to main content
15,895,841 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?
C#
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++)
            {
            }
        }       
    }
}
Posted
Updated 10-Nov-14 8:07am
v2

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
 
Share this answer
 
Comments
DamithSL 10-Nov-14 14:21pm    
5wd
Sergey Alexandrovich Kryukov 10-Nov-14 14:27pm    
Thank you, Damith.
—SA
Find Duplicate Files and Delete Them[^] article describe how you can find using Computed Hash code for files. Hope this helps.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Nov-14 14:27pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900