I'm trying to make a duplicate file detector (related question: Compare adjacent list items). My general structure is this:
- Scan directory and load needed details for each file into a DupInfo object
- Calculate CRCs for any that have a size match with another file
- Display any with matching size and CRCs (and optionally Base Directories), and allow the users to check which they want to delete (both manually, and using a button like "Check all first duplicates").
- Delete the selected files.
I'm having trouble with step 3. My data from steps 1 and 2 is in a List of DupInfo objects. Here is the class definition.
public class DupInfo
{
public string FullName { get; set; }
public long Size { get; set; }
public uint? CheckSum { get; set; }
public string BaseDirectory { get; set; }
public DupInfo(FileInfo file, Crc32 crc, int level)
{
FullName = file.FullName;
Size = file.Length;
CheckSum = crc.ComputeChecksum(File.ReadAllBytes(FullName));
BaseDirectory = FullName.Substring(0,FullName.NthIndexOf("\\",level));
}
public DupInfo(FileInfo file, int level)
{
FullName = file.FullName;
Size = file.Length;
BaseDirectory = FullName.Substring(0, FullName.NthIndexOf("\\", level));
}
}
My question is:
What is the best way to load data into a Listview object from a custom classs (DupInfo) list?
If a Listview isn't the best tool for displaying my duplicate sets, what is the best?
I would use a DataGrid bound to an
ObservableCollection<DupInfo>
.Codebehind:
And this is your DupInfo class. I've omitted the constructor to deal with it faster.
Result:
Update 1:
We don't have AddRange available but define an extenstion method for that:
This is our new version:
So we are loading our elements from a list .. we can use an array there or whatever else according to your needs.
But pay attention if you change the object on which our reference points to. In that case, you will have to use a DependencyProperty or implement INotifyPropertyChanged.
Your property will look like this:
Update 2:
XAML:
Codebehind:
And your updated DupInfo class:
That's about it. Good luck!