public async Task FindDuplicates() { FileMapBySize fileMapBySize = await FileMapBySize.GetNew( dispatcher, _rootDirectory, OnFileReadError, OnFileScanned); await BuildDuplicatesList(fileMapBySize); }
//we use a static GetNew instead of a public constructor //as some things like waiting on async operations //and setting event handlers cannot be done in the //constructor. the real constructor is kept private //and only ever called from this static GetNew method public static async Task <FileMapBySize> GetNew( IDispatcher d, IDirectory dir, DuplicateFileFinder.FileReadError error, EventHandler onscanned) { var fileMapBySize = new FileMapBySize(d); fileMapBySize.OnFileReadError = error; fileMapBySize.OnFileScanned = onscanned; await fileMapBySize.Populate(dir); return(fileMapBySize); }
private async Task BuildDuplicatesList(FileMapBySize fileMapBySize) { var fileMapByHashCode = new FileMapByString(); foreach (IFile file in fileMapBySize.GetPotentialDuplicates()) { try { string hash = await file.GetUniqueHash(dispatcher, OnHashProgress); List <IFile> list; if (!fileMapByHashCode.TryGetValue(hash, out list)) { list = new List <IFile>(); fileMapByHashCode.Add(hash, list); } list.Add(file); // Once we have two files with the same hashcode // we know that a duplicate has been found if (list.Count > 1) { if (!_duplicates.ContainsKey(hash)) { _duplicates.Add(hash, list); } NotifyDuplicateFound(list, hash, file); } } catch (Exception e) { NotifyFileReadError(file, e); } } }