58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace DuplicateFinder
|
|
{
|
|
internal class Program
|
|
{
|
|
private static string CalculateSha1(string filePath)
|
|
{
|
|
using (var sha1 = SHA1.Create())
|
|
{
|
|
using (var stream = File.OpenRead(filePath))
|
|
{
|
|
byte[] hashBytes = sha1.ComputeHash(stream);
|
|
return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
|
|
}
|
|
}
|
|
}
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
Console.Write("Enter directory path: ");
|
|
string directoryPath = Console.ReadLine();
|
|
|
|
Dictionary<string, List<string>> fileHashes = new Dictionary<string, List<string>>();
|
|
|
|
foreach (string filePath in Directory.EnumerateFiles(directoryPath, "*.*", SearchOption.AllDirectories))
|
|
{
|
|
string hash = CalculateSha1(filePath);
|
|
|
|
if (fileHashes.ContainsKey(hash))
|
|
{
|
|
fileHashes[hash].Add(filePath);
|
|
}
|
|
else
|
|
{
|
|
fileHashes[hash] = new List<string> { filePath };
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("\nDuplicate files:");
|
|
foreach (var group in fileHashes.Values.Where(v => v.Count > 1))
|
|
{
|
|
foreach (var filePath in group)
|
|
{
|
|
Console.WriteLine(filePath);
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
|
|
Console.ReadKey();
|
|
}
|
|
}
|
|
}
|