Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6095429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:48:39+00:00 2026-05-23T12:48:39+00:00

I am trying to obtain the same files using Intersect method from two folders.

  • 0

I am trying to obtain the same files using Intersect method from two folders.
123.xml files are the same (no change with content, date, size) in all the folders.

Scores\Content\123.xml
Scores\Content\hi.xml
Scores\123.xml

Power\Content\123.xml
Power\Content\helo.xml
Power\123.xml

This is from the C# code

        System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(path1);
        System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(path2);

        IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
        IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

        FileCompare myFileCompare = new FileCompare();

        bool areIdentical = list1.SequenceEqual(list2, myFileCompare);

        if (areIdentical == true)
        {
            Console.WriteLine("the two folders are the same");
        }
        else
        {
            Console.WriteLine("The two folders are not the same");
        }


        var queryCommonFiles = list1.Intersect(list2, myFileCompare);

The queryCommonFiles is only returning the 123.xml from Content folder, but not the other one.

This is the code from the FileCompare

class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
{

    public FileCompare() { }

    public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
    {
        return (f1.Name == f2.Name &&
                f1.Length == f2.Length);
    }

    // Return a hash that reflects the comparison criteria. According to the 
    // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must
    // also be equal. Because equality as defined here is a simple value equality, not
    // reference identity, it is possible that two or more objects will produce the same
    // hash code.
    public int GetHashCode(System.IO.FileInfo fi)
    {
        string s = String.Format("{0}{1}", fi.Name, fi.Length);
        return s.GetHashCode();
    }

}

EDIT:

var queryList1Only = (from file in list1
                                  select file).Except(list2, myFileCompare);

            Console.WriteLine("The following files are in list1 but not list2:\n");
            foreach (var v in queryList1Only)
            {
                Console.WriteLine(v.FullName);
            }


            var queryList2Only = (from file in list2
                                  select file).Except(list1, myFileCompare);

            Console.WriteLine("The following files are in list2 but not list1:\n");
            foreach (var v in queryList2Only)
            {
                Console.WriteLine(v.FullName);
            }

This produces as hi.xml for list1 and helo.xml for list2. And as I said only one 123.xml for the intersect method.

Any suggestions would be appreciated

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:48:40+00:00Added an answer on May 23, 2026 at 12:48 pm

    I just changed the Equals method to achieve the desired results

    class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
        {
    
            public FileCompare() { }
    
            public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
            {
                return (f1.Name == f2.Name && f1.Directory.Name == f2.Directory.Name && 
                        f1.Length == f2.Length);
            }
    
            // Return a hash that reflects the comparison criteria. According to the 
            // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must
            // also be equal. Because equality as defined here is a simple value equality, not
            // reference identity, it is possible that two or more objects will produce the same
            // hash code.
            public int GetHashCode(System.IO.FileInfo fi)
            {
                string s = String.Format("{0}{1}", fi.Name, fi.Length);
                return s.GetHashCode();
            }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to display a page in NSIS to obtain two different values. I
I use the StreamReader class to obtain XML for my GeoCoding process from Google.
I'm trying to do an XMLHttpRequest from a local file (file://) using JQuery.ajax to
I'm trying to draw a graph of the dependencies between C header files using
I have been trying this formula to obtain the azimut/bearing of two points of
I am trying to implement the MVP method for the first time, using WinForms.
I am trying to create a script to obtain data from a multiple databases
When trying to obtain an array of objects from an IEnumerable collection of objects
i am trying to call the MotionEvent.obtain Methode using the reflections library. But so
I am trying to obtain the value part of this syntax: [name:value] I got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.