[C# Helper]
Index Books FAQ Contact About Rod
[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]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

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

Title: Compare directories to see which files they have in common in C#

This example compares directories to see which files they have in common and which files are only in one directory. When you click the Compare button, the following code executes.

// Compare the files in each directory. private void btnCompare_Click(object sender, EventArgs e) { // Clear previous results. dgvFiles.Rows.Clear(); // Get sorted lists of files in the directories. string dir1 = txtDir1.Text; if (!dir1.EndsWith("\\")) dir1 += "\\"; string[] file_names1 = Directory.GetFiles(dir1); for (int i = 0; i < file_names1.Length; i++) { file_names1[i] = file_names1[i].Replace(dir1, ""); } Array.Sort(file_names1); string dir2 = txtDir2.Text; if (!dir2.EndsWith("\\")) dir2 += "\\"; string[] file_names2 = Directory.GetFiles(dir2); for (int i = 0; i < file_names2.Length; i++) { file_names2[i] = file_names2[i].Replace(dir2, ""); } Array.Sort(file_names2); // Compare. int i1 = 0, i2 = 0; while ((i1 < file_names1.Length) && (i2 < file_names2.Length)) { if (file_names1[i1] == file_names2[i2]) { // They match. Display them both. dgvFiles.Rows.Add(new Object[] { file_names1[i1], file_names2[i2] }); i1++; i2++; } else if (file_names1[i1].CompareTo(file_names2[i2]) < 0) { // Display the directory 1 file. dgvFiles.Rows.Add(new Object[] { file_names1[i1], null }); i1++; } else { // Display the directory 2 file. dgvFiles.Rows.Add(new Object[] { null, file_names2[i2] }); i2++; } } // Display remaining directory 1 files. for (int i = i1; i < file_names1.Length; i++) { dgvFiles.Rows.Add(new Object[] { file_names1[i], null }); } // Display remaining directory 2 files. for (int i = i2; i < file_names2.Length; i++) { dgvFiles.Rows.Add(new Object[] { null, file_names2[i] }); } }

The code first clears any previous results from its DataGridView control. It then uses the Directories.GetFiles method to get the names of the files in the first directory. It removes the directory name form the file names and uses Array.Sort to sort the names.

The code repeats these steps to get the names of the files in the second directory and sort them.

Next the code loops through the arrays comparing their entries. If two entries match, the code adds them both to the program's DataGridView control. If the files don't match, the program adds the one that comes alphabetically first to the DataGridView and increments its array's counter.

When one of the arrays runs out of entries, the program dumps any remaining names in the other array into the DataGridView.

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

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