0

I've a list of files and wish to check if these files are same (Content/Size)

IEnumerable<string> results = GetFiles("DirectorPath");    //This function returns the list of file names 
            foreach (string entry in results)
            {
                FileInfo fileInfo = new FileInfo(Path.Combine("C:\beta\source"), entry));
                //How I do check if the elements of **results** are identical or not ??
                //Problem is that the **results** may have (2,3,4) number of files, not same every time
            }

What I tried is

foreach (string entry in results)
            {
                FileInfo fileInfo = new FileInfo(Path.Combine("C:\beta\source"), entry));
                recentlyModFiles.Add(fileInfo); //Add the elements into another container and check for the content but will have to again use a forloop
            }
            

Can someone help me with a better way to do this?

Let me know if any additional data is required

Thanks.

Edit : My problem statement has multiple (2,3,4..) number of files which are not same every time and not about just 2 files.

m_alpha
  • 134
  • 13
  • 4
    This is usually done by computing a hash of the files and compare the result – Cid Sep 24 '20 at 08:50
  • 1
    Does this answer your question? [Calculate MD5 checksum for a file](https://stackoverflow.com/questions/10520048/calculate-md5-checksum-for-a-file) – Cid Sep 24 '20 at 08:51
  • 1
    Duplicate too : [How to compare 2 files fast using .NET?](https://stackoverflow.com/questions/1358510/how-to-compare-2-files-fast-using-net) – Cid Sep 24 '20 at 08:53
  • @Cid My question is not about comparing 2 files, it's about multiple files (2,3,4,5..) the count is not same every time.which makes a big difference. So don't mark it as a duplicate! – m_alpha Sep 24 '20 at 09:12
  • 1
    Well, the "solution" that Cid probably has in mind is to compute a hash for each of the N files you are dealing with and compare them. That makes this question a duplicate of "how to compare 2 files". Because if you can compare 2 you can compare N (with _very_ little change). – Fildor Sep 24 '20 at 09:28
  • 1
    Exactly @Fildor. If you know how to compare 2 elements if they are identical, you can compare a list of elements – Cid Sep 24 '20 at 10:34
  • You can even regroup the compared files using a dictionary, having as key the checksum and as value the list of the files – Cid Sep 24 '20 at 10:36

0 Answers0