0

I tried to create a simple little application to make it easier to create the Update files for my main project, by just comparing files in two more or less similar folders and telling me what files are different. But it always gives me the following output when testing two completely similar folders:

C:\Users\there\Desktop\Folder 2\1.txt

C:\Users\there\Desktop\Folder 2\2.txt

My code:

    private void FirstFolderBtn_Click(object sender, EventArgs e)
    {
        using (var fbd = new FolderBrowserDialog())
        {
            DialogResult result = fbd.ShowDialog();

            if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
            {
                FirstFolderTB.Text = fbd.SelectedPath;
            }
        }
    }

    private void SecondFolderButton_Click(object sender, EventArgs e)
    {
        using (var fbd = new FolderBrowserDialog())
        {
            DialogResult result = fbd.ShowDialog();

            if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
            {
                SecondFolderTB.Text = fbd.SelectedPath;
            }
        }
    }

    private void CompareBtn_Click(object sender, EventArgs e)
    {
        foreach(string file in Directory.GetFiles(FirstFolderTB.Text))
        {
            byte[] storedFileHash;
            using (var md5 = MD5.Create())
            {
                using (var stream = File.OpenRead(file))
                {
                    storedFileHash = md5.ComputeHash(stream);
                }

                using (var stream = File.OpenRead(file.Replace(FirstFolderTB.Text, SecondFolderTB.Text)))
                {
                    if(storedFileHash != md5.ComputeHash(stream))
                    {
                        ResultTB.AppendText(file.Replace(FirstFolderTB.Text, SecondFolderTB.Text) + "\n");
                    }
                }
            }
        }
    }

I think I am just being dumb right now and overseeing some dumb mistake but I would appreciate if someone could help.

Thanks, Jan

Community
  • 1
  • 1
Jan1902
  • 193
  • 1
  • 16
  • That is an explanation on how to get a MD5 hash, I have a problem doing that. So it is not a duplicate in my oppinion – Jan1902 Feb 03 '17 at 16:41
  • Perfect - now this is duplicate of 2 questions that exactly cover all the steps. – Alexei Levenkov Feb 05 '17 at 07:10
  • Thats wrong. I didn't know that my problem was the comparison, so I asked what the problem might be. If I knew that it was the comparison I would have looked at that. – Jan1902 Feb 05 '17 at 12:14

1 Answers1

0

Your bytes comparison is wrong

storedFileHash != md5.ComputeHash(stream)

this will compare references not the bytes.

Use SequenceEqual:

if (storedFileHash.SequenceEqual(md5.ComputeHash(stream)) == false) {
    ResultTB.AppendText(file.Replace(FirstFolderTB.Text, SecondFolderTB.Text) + "\n");
}

if missing add using System.Linq in the using section

Paweł Łukasik
  • 3,893
  • 1
  • 24
  • 36