Make a program that finds and removes 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 nodes_to_delete = new List();
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();
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 deletes 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 it 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.

The program then 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.

(For information on the DeleteFile method, see my post Move files and directories into the recycle bin (wastebasket), see how many items are in the recycle bin, and empty the recycle bin in C#.

After deleting the selected file nodes, some of the top-level nodes may have no more 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. (You could probably also remove top-level nodes that only have one child.)

After is has finished deleting the appropriate nodes, the program scroll to the top and makes the TreeView visible.

That finishes the explanation of the program’s main features. Download the example program to see additional details.

In the next and final post in this series, I’ll explain a big performance improvement.

   

This entry was posted in algorithms, cryptography, files, system, tools. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *