Hello,
I need some guidance with my third button. I have created a GUI that so far displays contents from from two folders by using folder browser dialog. All I want to do is compare the filenames between both folders and display the differences in a third listbox. I would like to add messages like "Differences"= string list. Any help would be appreciated.Below is my current code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog FBD = new FolderBrowserDialog();

            if (FBD.ShowDialog() == DialogResult.OK)
            {

                listBox1.Items.Clear();
                string[] files = Directory.GetFiles(FBD.SelectedPath);
                string[] dirs = Directory.GetDirectories(FBD.SelectedPath);

                foreach (string file in files)
                {
                    listBox1.Items.Add(file);
                }

                foreach (string dir in dirs)
                {
                    listBox1.Items.Add(dir);
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog FBD = new FolderBrowserDialog();
            if (FBD.ShowDialog() == DialogResult.OK)
            {
                listBox2.Items.Clear();
                string[] files = Directory.GetFiles(FBD.SelectedPath);
                string[] dirs = Directory.GetDirectories(FBD.SelectedPath);

                foreach (string file in files)
                {
                    listBox2.Items.Add(file);
                }

                foreach (string dir in dirs)
                {
                    listBox2.Items.Add(dir);
                }
            }
            {
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }
    }
}

What kind of differences do you want to see? For example you can use Linq's Except() to get a cross section of the two.

A couple things I've noticed:

  • You're loading the listboxes with the whole path. This makes finding the duplicates problematic at best.
  • You're duplicating the code to load the listboxes. A method taking the listbox as an argument will simplify this.

Since you're loading file names and directory names, using the DirectoryInfo class and getting all FileSystemInfo's in the directory, makes it easy to just load the names not the paths. The method to do this would look something like this:

    void LoadListbox(ListBox lb)
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        if (fbd.ShowDialog() == DialogResult.OK)
        {

            lb.DataSource = new DirectoryInfo(fbd.SelectedPath).GetFileSystemInfos().Select(x => x.Name).ToList();
        }
    }

Now using some more LINQ extensions, it is relatively easy to compile a list of any names not duplicated:

    private void buton3_Click(object sender, EventArgs e)
    {
        var list1 = listBox1.Items.Cast<string>();
        var list2 = listBox2.Items.Cast<string>();
        var exceptionsList1 = list1.Except(list2);
        var exceptionsList2 = list2.Except(list1);
        if (exceptionsList1.Count() == 0)
        {
            if (exceptionsList2.Count() == 0)
            {
                return;
            }
            listBox3.DataSource = exceptionsList2.ToList();
            return;
        }
        if(exceptionsList2.Count() == 0)
        {
            listBox3.DataSource = exceptionsList1.ToList();
            return;
        }
        listBox3.DataSource = exceptionsList1.Concat(exceptionsList2).ToList();
    }

Edited 10 Months Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.