using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace FileManipulation
{
internal class FolderCompare
{
private static readonly string folderOne = "D:\\Folder1";
private static readonly string folderTwo = "D:\\Folder2";
/// <summary>
/// Compares two folder files and shows which files are same
/// and which files are not.
/// Doesn't compare content but file names.
/// </summary>
internal void Compare()
{
string[] folderOneFiles = Directory.GetFiles(folderOne,"*.*",SearchOption.AllDirectories);
var folderOneFileNames = folderOneFiles.Select(file => new FileInfo(file).Name);
string[] folderTwoFiles = Directory.GetFiles(folderTwo, "*.*", SearchOption.AllDirectories);
var folderTwoFileNames = folderTwoFiles.Select(file => new FileInfo(file).Name);
HashSet<string> listOfFilesSame = new HashSet<string>();
HashSet<string> listOfFilesNotSameOfOne = new HashSet<string>();
HashSet<string> listOfFilesNotSameOfTwo = new HashSet<string>();
// add folder one files to 'listOfFilesSame'
// and intersect with 'folderTwoFileNames'
listOfFilesSame.UnionWith(folderOneFileNames);
listOfFilesSame.IntersectWith(folderTwoFileNames);
// add folder one files and intersect with 'listOfFilesSame'
listOfFilesNotSameOfOne.UnionWith(folderOneFileNames);
listOfFilesNotSameOfOne.ExceptWith(listOfFilesSame);
// add folder two files and intersect with 'listOfFilesSame'
listOfFilesNotSameOfTwo.UnionWith(folderTwoFileNames);
listOfFilesNotSameOfTwo.ExceptWith(listOfFilesSame);
// show the lists
// show the list of same in both
Console.WriteLine("\n\nBelow is the list of same files");
int count = 1;
foreach (string name in listOfFilesSame)
{
Console.WriteLine("{0}. {1}", count, name);
count++;
}
// show the list of different files in folder one
Console.WriteLine("\n\nBelow is the list of different files in folder one");
count = 1;
foreach (string name in listOfFilesNotSameOfOne)
{
Console.WriteLine("{0}. {1}", count, name);
count++;
}
// show the list of different files in folder two
Console.WriteLine("\n\nBelow is the list of different files in folder two");
count = 1;
foreach (string name in listOfFilesNotSameOfTwo)
{
Console.WriteLine("{0}. {1}", count, name);
count++;
}
}
}
}
Find Different and Same Files of Two Folders Dot Net (.NET)
