MD5 Test Scanner

//(c) Abe Woodhouse
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.



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;
using System.Security.Cryptography;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            ScanFiles(new DirectoryInfo(@"C:\Users\EH\My Documents\")); // this line is specifieing where to being the scan
        }

        static void ScanFiles(DirectoryInfo directory) // i have made a static function which will define
        {
            FileInfo[] files = directory.GetFiles(); // here i have created a file information array
            for (int i = 0; i < files.Length; i++) // here i have started a for loop with an argument which will keep listing files
            {
                Console.WriteLine(files[i].FullName); // this line gives the fullname of the file once it has been scanned

                // in order to do the MD5 of each file i had to seek help online from the link:http://stackoverflow.com/questions/10520048/calculate-md5-checksum-for-a-file which helped me with the following code.
                using (var md5 = MD5.Create()) // here i am creating an MD5 of the fiels which ahve been scanned
                {
                    using (var stream = File.OpenRead(files[i].FullName)) // this line of code opens the file and reads of the fullname of the file
                    {
                        string hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower(); //this line of code returns the hash value
                        if (hash == "d41d8cd98f00b204e9800998ecf8427e") // because i wanted to test the comparison of a file on the system i made this if loop with a static string value of an MD5
                        {
                            MessageBox.Show("match");// in this line of code i am telling to form that if the match is found display this message to the user
                        }
                    }
                }

            }

            DirectoryInfo[] directories = directory.GetDirectories(); // here i have made a directory info array which relates to a directories variable which will get all directories scanned
            for (int i = 0; i < directories.Length; i++) // this makes sure that if the variable i is less than the current length of directories it will keep looping until all files and directories are scanned
            {
                ScanFiles(directories[i]);
            }


        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // MessageBox.Show(e.Result.ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync(textBox1.Text);
        }
    }
}


Learn More :