Rudy Escamilla

Rudy Escamilla

  • NA
  • 4
  • 1.4k

Switching from openfiledialog to folderbrowsedialog

Feb 8 2016 10:19 AM

I created a windows app that will compare strings from two different files. However, what I wanted was to open the folder, display the contents in listbox 1. Open folder 2 and display contents in list box 2. The differences will display in listbox 3.  However, I am using openfiledialog and I need to use folderbrowse dialog. I seem to have gotten the string compare portion that way I wanted however I want to compare the contents(filenames) in individual folders. How can I switch from openfiledialog to folderbrowsedialog and still have the compare work? It maybe something straight forward but I am missing it. Any help would be appreciated.

 

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(Path.GetFileName(file));

                }

 

                foreach (string dir in dirs)

                {

                    listBox1.Items.Add(dir);

                }

 

            }

        }

       

        */

 

            OpenFileDialog f = new OpenFileDialog();

            if (f.ShowDialog() == DialogResult.OK)

            {

                listBox1.Items.Clear();

 

                List<string> lines = new List<string>();

                using (StreamReader r = new StreamReader(f.OpenFile()))

                {

                    string line;

                    while ((line = r.ReadLine()) != null)

                    {

                        listBox1.Items.Add(line);

 

                    }

                }

            }

 

 

 

        }

 

 

        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(Path.GetFileName(file));

                }

 

                foreach (string dir in dirs)

                {

                    listBox2.Items.Add(dir);

                }

            }

            {

            }

        }

       */

            OpenFileDialog f = new OpenFileDialog();

            if (f.ShowDialog() == DialogResult.OK)

            {

                listBox2.Items.Clear();

 

                List<string> lines = new List<string>();

                using (StreamReader r = new StreamReader(f.OpenFile()))

                {

                    string line;

                    while ((line = r.ReadLine()) != null)

                    {

                        listBox2.Items.Add(line);

 

                    }

                }

            }

 

        }

 

 

        private void button3_Click(object sender, EventArgs e)

        {

 

 

            foreach (string item in listBox1.Items)

            {

 

                if (listBox2.Items.Contains(item))

                {

                    listBox3.Items.Add("Similar File Names: ");

                    listBox3.Items.Add(item);  // similar items

 

                }

 

                else

                {

                    listBox3.Items.Add("Dissimilar File Names: ");

                    // listBox3.Items.Add(item);  // dissimilar items

 

                }

 

            }

        }

 

 

        private void button4_Click(object sender, EventArgs e)

        {

            listBox1.Items.Clear();

            listBox2.Items.Clear();

            listBox3.Items.Clear();

 

        }

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

 

 

 

 

 

 

    }

 

 

}

 

Answers (2)