Title: Find duplicate files in C#, Part 3 of 4
The last two posts described an application that searches for duplicate files and removes them. This post explains how the program removes the files when you click the Delete Selected button. When you click the button, the following code executes.
// Delete the selected files.
private bool Deleting = false;
private void btnDeleteSelected_Click(object sender, EventArgs e)
{
Deleting = true;
trvFiles.Visible = false;
lblNumDuplicates.Text = "";
picImage.Visible = false;
picImage.Image = null;
rchText.Visible = false;
Cursor = Cursors.WaitCursor;
Refresh();
try
{
// Make a list of nodes to delete.
List<TreeNode> nodes_to_delete = new List<TreeNode>();
foreach (TreeNode hash_node in trvFiles.Nodes)
{
foreach (TreeNode file_node in hash_node.Nodes)
{
// See if the file's node is checked.
if (file_node.Checked)
nodes_to_delete.Add(file_node);
}
}
// Delete the selected nodes and their files.
foreach (TreeNode file_node in nodes_to_delete)
{
// Get the FileInfo from the Tag property.
FileInfo file_info = file_node.Tag as FileInfo;
// Move the file into the recycle bin.
DeleteFile(file_info.FullName);
// Remove the file from the TreeView.
file_node.Remove();
}
// Make a list of size nodes with no remaining children.
nodes_to_delete = new List<TreeNode>();
foreach (TreeNode hash_node in trvFiles.Nodes)
{
if (hash_node.Nodes.Count == 0)
nodes_to_delete.Add(hash_node);
}
// Delete the size nodes with no remaining children.
foreach (TreeNode hash_node in nodes_to_delete)
{
hash_node.Remove();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// Scroll to the top.
if (trvFiles.Nodes.Count > 0)
trvFiles.Nodes[0].EnsureVisible();
trvFiles.Visible = true;
Deleting = false;
Cursor = Cursors.Default;
}
}
If the code deleted a TreeView node that happens to be selected, the TreeView control selects a new node. That makes the program display the selected file's contents. The whole process slows things down and can be really silly with the program displaying a bunch of files as quickly as it can.
To prevent that, the program defines the variable Deleting. When this value is true, the program doesn't display the selected file. (See the previous post to see how that part works.)
To get ready, the btnDeleteSelected_Click event handler sets Deleting to true. It also hides the picImage and rchText controls that the program uses to display files, and it hides the TreeView control so the control doesn't need to update itself every time a node is deleted.
Next, the code loops through the top-level TreeView nodes. For each top-level node, the code loops through its child nodes. If a child node is checked, the program adds it to the nodes_to_delete list.
After examining all of the tree's nodes, the program loops through the list of nodes to delete. For each node, it gets the FileInfo object stored in the node's Tag property. It calls the DeleteFile method to move the file into the recycle bin and then removes the node from the TreeView control.
(For information on the DeleteFile method, see my post Manage the recycle bin (wastebasket) in C#.)
After deleting the selected file nodes, some of the top-level nodes may have no remaining children so they should be removed. The program loops through the top-level nodes and builds a list of those with no children. It then loops through the list and removes those nodes from the TreeView control. (You could probably also remove top-level nodes that only have one child.)
After is has finished deleting the appropriate nodes, the program scrolls to the top and makes the TreeView visible.
That finishes the explanation of the program's main features. In the next and final post in this series, I'll explain a big performance improvement.
Download the example to experiment with it and to see additional details.
|