0

I need help trying to compare files that a user has selected. I cant seem to figure out exactly how to do it. I have the following code so far. The language is C#, and its a GUI application. At first I tried to assign a bool type variable and compare the files that way, but the compare button wouldn't work. I would really appreciate some kind of input from someone.

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


namespace FileComparison
{
public partial class Form1 : Form
{
    long fileSize1, fileSize2;
    FileInfo fileInfo1, fileInfo2;
    string fileName1, fileName2;
    double ratio;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            fileName1 = openFileDialog1.FileName;
            label3.Text = label3.Text + fileName1;

            fileInfo1 = new FileInfo(fileName1);
            fileSize1 = fileInfo1.Length;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog2 = new OpenFileDialog();

        if (openFileDialog2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            fileName2 = openFileDialog2.FileName;
            label4.Text = label4.Text + fileName2;

            fileInfo2 = new FileInfo(fileName2);
            fileSize2 = fileInfo2.Length;
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {

    }
  • What do you mean by "the compare button wouldn't work"? Is the event never called? Is it called, but supposed to do *something* it does not? I assume this is `button3_Click`. What code is supposed to be in there? – Broots Waymb Jun 04 '20 at 12:44
  • What should be result of the comparison? Just true/false - same or different? This simple comparison can be done with hash functions e.g. SHA256(file1) == SHA256(file2) – mybrave Jun 04 '20 at 14:11

2 Answers2

0

If you would like to compare by file sizes. You could do something like this:

Change the console lines to your labelcontents to display on label.

 private void btn3_Click(object sender, EventArgs e)
        {
            if(fileSize1 > fileSize2)
            {
                Console.WriteLine($"File 1 (with filesize {fileSize1}) is bigger than file 2 (with filesize {fileSize2}).");
            }
            else if(fileSize1 == fileSize2)
            {
                Console.WriteLine($"File 1 (with filesize {fileSize1}) is equal to file 2 (with filesize {fileSize2})");
            }
            else
            {
                Console.WriteLine($"File 2 (with filesize {fileSize2}) is bigger than file 1 (with filesize {fileSize1}).");
            }
        }
axtck
  • 3,707
  • 2
  • 10
  • 26
-1

if you you want to compare file when click btn3_Click. - First Just create individual function with two Parameter.In Function we match the file content length. - Call this function in btn_click. Like this

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

namespace StackProject
{
    public partial class Form1 : Form
    {
        long fileSize1, fileSize2;
        FileInfo fileInfo1, fileInfo2;
        string fileName1, fileName2;
        double ratio;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fileName1 = openFileDialog1.FileName;
                label3.Text = label3.Text + fileName1;

                //fileInfo1 = new FileInfo(fileName1);
                //fileSize1 = fileInfo1.Length;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog2 = new OpenFileDialog();

            if (openFileDialog2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fileName2 = openFileDialog2.FileName;
                label4.Text = label4.Text + fileName2;

                //fileInfo2 = new FileInfo(fileName2);
                //fileSize2 = fileInfo2.Length;
            }  
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (FileEquals(fileName1, fileName2))
            {
                MessageBox.Show("Same File");
            }
            else { MessageBox.Show("Different File"); }

        }


        private bool FileEquals(string path1, string path2)
        {
            byte[] file1 = File.ReadAllBytes(path1);
            byte[] file2 = File.ReadAllBytes(path2);
            if (file1.Length == file2.Length)
            {
                for (int i = 0; i < file1.Length; i++)
                {
                    if (file1[i] != file2[i])
                    {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }
    }
}