[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Find duplicate files in C#, Part 2 of 4

[duplicate files]

The previous post Find duplicate files in C#, Part 1 of 4 explained how the example uses a LINQ query to select files grouped by hash code. This post explains how the program displays the files' contents and how the Select Duplicate button works.

When you click on a node in the TreeView control, the following AfterSelect event handler executes.

// Display the clicked file. private void trvFiles_AfterSelect(object sender, TreeViewEventArgs e) { if (Deleting) return; // Hide the display controls. rchText.Visible = false; picImage.Visible = false; Refresh(); // Do nothing for size nodes. if (e.Node.Level == 0) return; // Get the file's information. FileInfo file_info = e.Node.Tag as FileInfo; switch (file_info.Extension.ToLower()) { case ".txt": // Text files. case ".html": case ".cs": case ".csproj": case ".resx": case ".xml": case ".xaml": case ".config": rchText.Text = File.ReadAllText(file_info.FullName); rchText.Visible = true; break; case ".rtf": // Rich text. rchText.LoadFile(file_info.FullName); rchText.Visible = true; break; case ".jpg": // Image files. case ".jpeg": case ".gif": case ".png": case ".tiff": case ".bmp": picImage.Image = LoadBitmapUnlocked(file_info.FullName); picImage.Visible = true; break; default: rchText.Text = "Unknown file extension " + file_info.Extension; rchText.Visible = true; break; } }

The event handler executes sometimes while the program is manipulating the TreeView control. To prevent it from displaying unwanted file previews, the event handler checks the Deleting variable and exits if Deleting is true.

The code then hides the rchText and picImage controls that it will use to display file previews.

If the level of the node selected is 0, then this is a top-level hash code node. No file was selected so there's nothing to show and the event handler exits.

If this is a file's node, the program gets the FileInfo object stored in the node's Tag property (see the previous post). It checks the FileInfo object's Extension property and takes action depending on what kind of file it represents.

If this is a text file, the code uses File.ReadAllText to get the file's contents and then displays them as text in the RichTextBox named rchText.

If this is a Rich Text file, the code uses the rchText control's LoadFile method to load the file into the RichTextBox.

If this is a graphics file, the code uses the LoadBitmapUnlocked method to load the file without locking it. To see how that method works, download the example and see the post Load images without locking their files in C#.

When you click the Select Duplicates button, the following code executes.

// Select all but the first file in each group. private void btnSelectDuplicates_Click(object sender, EventArgs e) { foreach (TreeNode hash_node in trvFiles.Nodes) { hash_node.Checked = false; hash_node.Nodes[0].Checked = false; for (int i = 1; i < hash_node.Nodes.Count; i++) hash_node.Nodes[i].Checked = true; } }

This code loops through the TreeView control's Nodes collection. That collection only lists the control's top-level nodes so the loop only covers them.

The loop unchecks the top-level nodes and the first child nodes of the top-level nodes. It then loops through the top-level node's other child nodes (that's why the loop starts with index 1), checking them. When the code is finished, the top-level nodes and one child each are unchecked. The rest of the nodes are checked.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.