Vorgehensweise: Vergleichen des Inhalts von zwei Ordnern (LINQ) (C#)How to: Compare the Contents of Two Folders (LINQ) (C#)

Dieses Beispiel zeigt drei Verfahren zum Vergleichen von zwei Dateilisten:This example demonstrates three ways to compare two file listings:

  • Durch Abfragen eines booleschen Werts, der angibt, ob die zwei Dateilisten identisch sind.By querying for a Boolean value that specifies whether the two file lists are identical.

  • Durch Abfragen der Schnittmenge, um die Dateien abzurufen, die sich in beiden Ordnern befinden.By querying for the intersection to retrieve the files that are in both folders.

  • Durch Abfragen der Unterschiedsmenge, um die Dateien abzurufen, die sich in einem Ordner befinden, aber nicht im anderen.By querying for the set difference to retrieve the files that are in one folder but not the other.

    Hinweis

    Die hier gezeigten Verfahren können zum Vergleichen von Sequenzen von Objekten eines beliebigen Typs angepasst werden.The techniques shown here can be adapted to compare sequences of objects of any type.

Die hier gezeigte FileComparer-Klasse veranschaulicht, wie eine benutzerdefinierte Vergleichsklasse zusammen mit den Standardabfrageoperatoren verwendet wird.The FileComparer class shown here demonstrates how to use a custom comparer class together with the Standard Query Operators. Die Klasse ist nicht für die Verwendung in realen Szenarios vorgesehen.The class is not intended for use in real-world scenarios. Sie verwendet nur den Namen und die Länge jeder Datei in Bytes, um zu bestimmen, ob die Inhalte der einzelnen Ordner identisch sind.It just uses the name and length in bytes of each file to determine whether the contents of each folder are identical or not. In einem realen Szenario sollten Sie diese Vergleichsklasse ändern, um eine gründlichere Überprüfung auf Gleichheit durchzuführen.In a real-world scenario, you should modify this comparer to perform a more rigorous equality check.

BeispielExample

namespace QueryCompareTwoDirs  
{  
    class CompareDirs  
    {  

        static void Main(string[] args)  
        {  

            // Create two identical or different temporary folders   
            // on a local drive and change these file paths.  
            string pathA = @"C:\TestDir";  
            string pathB = @"C:\TestDir2";  

            System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);  
            System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);  

            // Take a snapshot of the file system.  
            IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);  
            IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);  

            //A custom file comparer defined below  
            FileCompare myFileCompare = new FileCompare();  

            // This query determines whether the two folders contain  
            // identical file lists, based on the custom file comparer  
            // that is defined in the FileCompare class.  
            // The query executes immediately because it returns a bool.  
            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");  
            }  

            // Find the common files. It produces a sequence and doesn't   
            // execute until the foreach statement.  
            var queryCommonFiles = list1.Intersect(list2, myFileCompare);  

            if (queryCommonFiles.Count() > 0)  
            {  
                Console.WriteLine("The following files are in both folders:");  
                foreach (var v in queryCommonFiles)  
                {  
                    Console.WriteLine(v.FullName); //shows which items end up in result list  
                }  
            }  
            else  
            {  
                Console.WriteLine("There are no common files in the two folders.");  
            }  

            // Find the set difference between the two folders.  
            // For this example we only check one way.  
            var queryList1Only = (from file in list1  
                                  select file).Except(list2, myFileCompare);  

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

            // Keep the console window open in debug mode.  
            Console.WriteLine("Press any key to exit.");  
            Console.ReadKey();  
        }  
    }  

    // This implementation defines a very simple comparison  
    // between two FileInfo objects. It only compares the name  
    // of the files being compared and their length in bytes.  
    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();  
        }  
    }  
}  

Kompilieren des CodesCompiling the Code

Erstellen Sie ein neues Projekt, das auf die .NET Framework-Version 3.5 oder höher ausgelegt ist, mit einer Referenz zu System.Core.dll und using-Direktiven für System.Linq- und System.IO-Namespaces.Create a project that targets the .NET Framework version 3.5 or higher, with a reference to System.Core.dll and using directives for the System.Linq and System.IO namespaces.

Siehe auchSee Also

LINQ to Objects (C#)LINQ to Objects (C#)
LINQ und Dateiverzeichnisse (C#)LINQ and File Directories (C#)