开发者

c# File Checksum Function

开发者 https://www.devze.com 2023-04-11 05:06 出处:网络
I\'m creating this function to check if all my files are still correct or wether some files are missing.

I'm creating this function to check if all my files are still correct or wether some files are missing.

So far I've managed to obtain a list with Files from the root directory, and another list with all it's hashes.

I also have managed to create a healthy hash text file. which contains the Hash 3 tabs (so it's easier readable in notepad) and then the file name from root.

eg.

3914ea0985f3f67a8204685beb6d1be6            \file1.extension
2ed432f68ab6ebfc32664409482f0de2            \folder1\file2.extension

Each ends up in a seprate list so now I have 4 lists.

I was wondering whether i should use dictionaries instead to reduce 4 lists to 2 dictionaries.

Thus the filename (plus any subdirectory) would be the key, and the value would be the hash.

KEY                                 VALUE
\file1.extension                    3914ea0985f3f67a8204685beb6d1be6
\folder1\file2.extension            2ed432f68ab6ebfc32664409482f0de2

My assumption is that by doing this, I can check for missing files and delete those keys from the dictionary with the healthy hashes. So that I can check existing files equally against each other. (just based on index).

Below this my current codes to get the stuff required.

This gets the file list:

    public List<string> Get_FileList(string root)
    {
        List<string> FileList = Directory.GetFiles(root, "*.*", SearchOption.AllDirectories).Where(name => 
        { 
            return
                !(name.EndsWith("dmp") || name.EndsWith("jpg") ||                               //exclude dmp and image files
                name.EndsWith("FileChecker.exe"));                                             //exclude myself
        }).ToList();

        return FileList;
    }

This gets the hashes:

    public List<string> Get_FileHash(List<string> FileList)
    {
        List<string> FileHash = new List<string>();
        foreach (string FileName in FileList)
        {
            FileStream file = new FileStream(FileName, FileMode.Open, FileAccess.Read);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            FileHash.Add(sb.ToString());
        }
        return FileHash;
    }

This gets the healthy hashes:

public void Get_HealthyHash(string file,开发者_Go百科 out List<string> Healthy_FileList, out List<string> Healthy_HashList) 
    {
        string resource= "FileCheckSum.Resources." + file;

        Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);

        StreamReader reader = new StreamReader(stream);

        Healthy_FileList = new List<string>();
        Healthy_HashList = new List<string>();

        string line;
        while ((line = reader.ReadLine()) != null)
        {
            string[] items = line.Split(new string[] { "\t\t\t" }, StringSplitOptions.RemoveEmptyEntries);
            Healthy_FileList.Add(items[1]);
            Healthy_HashList.Add(items[0]);
        }

    }

And to determine the missing files I use this:

IEnumerable<string> Dif_File_list = Healthy_FileList.Except(FileList.Select(name => name.Replace(root, "")));

I have to remove the root here since the healthy hash file does not have the path from C:\

So as you can see 4 lists, (well 5 after getting the differences).

My question:

How can/should I continue checking the existing files if they are valid from this point? without the missing files interfering.

Any help, improvement on my functions or pointers to continue would be appreciated. NOTE All the code given here works! Be it slow with a great amount of files, as I have not added any sort of threading to make it faster.


Make an entity class MyFileInfo with string properties FileName and HashValue. Implement IEqualityComparer, override Equals and GetHashCode methods.

Then load healthy List< MyFileInfo> from file and build toExamine List< MyFileInfo> from current directory.

Use LINQ methods to find differences between lists.

Look here LINQ Distinct, Except, Contains, Union, Intersect and IEqualityComparer

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号