Advertisement
inoonan

FileSyncr

Mar 8th, 2020
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace FileSyncr
  7. {
  8.     public class FileSyncr
  9.     {
  10.         public string PathToPrimary { get; set; }
  11.         public string PathToSecondary { get; set; }
  12.  
  13.         public void RunSyncProccess()
  14.         {
  15.             CompaireDirectories();
  16.         }
  17.  
  18.         private void CompaireDirectories()
  19.         {
  20.             // Create two identical or different temporary folders
  21.             // on a local drive and change these file paths.
  22.             string pathA = PathToPrimary;
  23.             string pathB = PathToSecondary;
  24.  
  25.             DirectoryInfo dir1 = new DirectoryInfo(pathA);
  26.             DirectoryInfo dir2 = new DirectoryInfo(pathB);
  27.  
  28.             // Take a snapshot of the file system.
  29.             IEnumerable<FileInfo> list1 = dir1.GetFiles("*.*", SearchOption.AllDirectories);
  30.             IEnumerable<FileInfo> list2 = dir2.GetFiles("*.*", SearchOption.AllDirectories);
  31.  
  32.             //A custom file comparer defined below
  33.             FileCompare myFileCompare = new FileCompare();
  34.  
  35.             // This query determines whether the two folders contain
  36.             // identical file lists, based on the custom file comparer
  37.             // that is defined in the FileCompare class.
  38.             // The query executes immediately because it returns a bool.
  39.             bool areIdentical = list1.SequenceEqual(list2, myFileCompare);
  40.  
  41.             if (areIdentical == true)
  42.             {
  43.                 Console.WriteLine("-----------------------------------------------------");
  44.                 Console.WriteLine("the two folders are the same");
  45.             }
  46.             else
  47.             {
  48.                 Console.WriteLine("-----------------------------------------------------");
  49.                 Console.WriteLine("The two folders are not the same");
  50.             }
  51.  
  52.             // Find the common files. It produces a sequence and doesn't
  53.             // execute until the foreach statement.
  54.             var queryCommonFiles = list1.Intersect(list2, myFileCompare);
  55.  
  56.             if (queryCommonFiles.Any())
  57.             {
  58.                 Console.WriteLine("-----------------------------------------------------");
  59.                 Console.WriteLine("The following files are in both folders:");
  60.                 Console.WriteLine("-----------------------------------------------------");
  61.                 foreach (var v in queryCommonFiles)
  62.                 {
  63.                     Console.WriteLine(v.FullName); //shows which items end up in result list
  64.                 }
  65.             }
  66.             else
  67.             {
  68.                 Console.WriteLine("-----------------------------------------------------");
  69.                 Console.WriteLine("There are no common files in the two folders.");
  70.             }
  71.  
  72.             // Find the set difference between the two folders.
  73.             // For this example we only check one way.
  74.             var queryList1Only = (from file in list1
  75.                                   select file).Except(list2, myFileCompare);
  76.  
  77.             Console.WriteLine("-----------------------------------------------------");
  78.             Console.WriteLine("The following files produce a descrepency:");
  79.             Console.WriteLine("-----------------------------------------------------");
  80.             foreach (var v in queryList1Only)
  81.             {
  82.                 Console.WriteLine(v.FullName);
  83.             }
  84.  
  85.             // Keep the console window open in debug mode.
  86.             Console.WriteLine("Press any key to exit.");
  87.             Console.ReadKey();
  88.         }
  89.     }
  90. }
  91.  
  92. // This implementation defines a very simple comparison
  93. // between two FileInfo objects. It only compares the name
  94. // of the files being compared and their length in bytes.
  95. internal class FileCompare : IEqualityComparer<FileInfo>
  96. {
  97.     public FileCompare()
  98.     {
  99.     }
  100.  
  101.     public bool Equals(FileInfo f1, FileInfo f2)
  102.     {
  103.         return (f1.Name == f2.Name && f1.Length == f2.Length && f1.LastWriteTime == f2.LastWriteTime);
  104.     }
  105.  
  106.     // Return a hash that reflects the comparison criteria. According to the
  107.     // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must
  108.     // also be equal. Because equality as defined here is a simple value equality, not
  109.     // reference identity, it is possible that two or more objects will produce the same
  110.     // hash code.
  111.     public int GetHashCode(FileInfo fi)
  112.     {
  113.         string s = $"{fi.Name}{fi.Length}{fi.LastWriteTime}";
  114.         return s.GetHashCode();
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement