2 minute read

In this blog post, we’ll create a simple Windows Forms application that allows users to compare files in two directories. This tool is handy for identifying files present in one directory but missing in another, especially when dealing with large datasets or project files.

For the complete project and more details, check out my GitHub repository: CompareFiles.

Running Project

Project Overview

We’ll use C# and the Windows Forms framework to build our application. The main features include:

  • Selecting two directories for comparison.
  • Specifying a file extension filter.
  • Displaying the results of the comparison.
  • Copying results to the clipboard.

Code Explanation

Browsing for Directories

Users can select directories using the FolderBrowserDialog. The following methods handle the button clicks for selecting the first and second directories:

private void buttonBrowse_Click(object sender, EventArgs e)
{
    var openFileDialog1 = new FolderBrowserDialog();

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        textBoxFirstDirectoryPath.Text = openFileDialog1.SelectedPath;
    }
}

private void buttonBrowseSecond_Click(object sender, EventArgs e)
{
    var openFileDialog1 = new FolderBrowserDialog();

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        textBoxSecondDirectoryPath.Text = openFileDialog1.SelectedPath;
    }
}

Comparing Files

The comparison logic is executed in the buttonCompare_Click method, which is triggered when the user clicks the “Compare” button. We call the SearchFilesInDirectory method to perform the comparison asynchronously:

private async void buttonCompare_Click(object sender, EventArgs e)
{
    var searchFilesInDirectory = await SearchFilesInDirectory(textBoxFirstDirectoryPath.Text, textBoxSecondDirectoryPath.Text);
    var searchString = string.Join(Environment.NewLine, searchFilesInDirectory.ToArray());
    textBoxResult.Text = searchString;
}

Searching for Files

The SearchFilesInDirectory method takes two directory paths and compares the files based on a specified extension:

private async Task<IEnumerable<string>> SearchFilesInDirectory(string s, string s1)
{
    var searchPattern = !string.IsNullOrEmpty(textBoxExtension.Text) ? "*." + textBoxExtension.Text : "*.*";
    var firstDirSearch = await DirSearch(s, searchPattern);
    var secondDirSearch = await DirSearch(s1, searchPattern);
    var searchResult = firstDirSearch.Except(secondDirSearch);

    return searchResult.ToList();
}

Directory Search Logic

The DirSearch method recursively searches through a directory and collects file names that match the search pattern:

private static Task<List<string>> DirSearch(string sDir, string searchPattern)
{
    var filesName = new List<string>();
    try
    {
        filesName.AddRange(Directory.GetFiles(sDir, searchPattern).Select(f => Path.GetFileName(f)));

        foreach (var d in Directory.GetDirectories(sDir))
        {
            DirSearch(d, searchPattern);
        }
    }
    catch (Exception exception)
    {
        // Optionally log the exception
    }

    return Task.FromResult(filesName);
}

Copying Results to Clipboard

To make it easier for users, we provide functionality to copy the results to the clipboard:

private void buttonCopy_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(textBoxResult.Text))
        Clipboard.SetText(textBoxResult.Text);
}

Conclusion

This simple file comparison tool demonstrates how to build a Windows Forms application in C#. By leveraging asynchronous programming, we ensure that the application remains responsive during file operations.

Feel free to enhance this tool further, perhaps by adding features like sorting results, more sophisticated filtering options, or even integrating with cloud storage solutions. Happy coding!

Leave a comment