I am trying to hash video files in order to get a list of duplicates. I have looked here here and here, which is where I got some of the code. But for some reason, my method breaks at this line.
byte[] hash = md5.ComputeHash(fs);
I have tried changing the method, doing garbage collection manually, and substituting md5.ComputeHash() with HashAlgorithm.ComputeHash() with no luck. Here is my code :
Main Class Code
Console.WriteLine("Please enter a directory path :");
string path = Console.ReadLine();
loadFiles load = new loadFiles(path, "video");
videoFiles video = new videoFiles(path);
video.removeDuplicates(load.files);
Console.WriteLine("Done");
Class that loads file into arrays
private List<string> videoExt = new List<string>() { ".mp4", ".avi", ".mkv", ".srt", ".t" };
private string filetype;
public loadFiles(string path, string filetype)
{
this.path = path;
this.filetype = filetype;
getFiles();
getDirectories();
}
public FileInfo[] getFiles()
{
DirectoryInfo d = new DirectoryInfo(path);
if (filetype == "audio")
{
files = d.GetFiles("*", SearchOption.AllDirectories).Where(x => audioExt.Contains(x.Extension)).ToArray();
}
else if (filetype == "video")
{
files = d.GetFiles("*", SearchOption.AllDirectories).Where(x => videoExt.Contains(x.Extension)).ToArray();
}
return files;
}
Method that searches for duplicates and adds them to list
public void removeDuplicates(FileInfo[] files)
{
List<byte[]> hashes = new List<byte[]>();
List<string> duplicates = new List<string>();
foreach (FileInfo file in files)
{
using (FileStream fs = file.OpenRead())
{
using (MD5 md5 = MD5.Create())
{
byte[] hash = md5.ComputeHash(fs);
if (hashes.Contains(hash))
duplicates.Add(file.FullName);
else
hashes.Add(hash);
}
}
}