Click here to Skip to main content
15,889,834 members
Articles / Programming Languages / C#

Multithreaded File/Folder Finder

Rate me:
Please Sign up or sign in to vote.
4.19/5 (18 votes)
26 May 2010CPOL17 min read 110.5K   5.6K   125  
File Find is fast, especially if you have multiple physical drives; version 2.1.0.17.
using System;
using System.Collections;
using System.Windows.Forms;

namespace FileFind
{
    /// <summary>
    ///   User interface to display a list of folders and disk drives to be excluded
    /// when a search is performed. The information is stored in ConfigInfo.
    /// </summary>
    public partial class IgnoreFolders : Form
    {
        private ConfigInfo configInfo = new ConfigInfo();
        private bool isDirty = false;   //determines if ConfigInfo class is to be updated
                                        //when this form closes

        public IgnoreFolders()
        {
            InitializeComponent();
            this.Font = configInfo.UseFont;
        }

        void AddNewFolder(string newIgnore)
        {
            string str = newIgnore.Replace('\\', '/');
            int index = listBox1.FindStringExact(str, -1);    // Search starting from index -1:
            if (index == -1)
            {
                listBox1.Items.Add(str);
                isDirty = true;
            }
        } //ends void AddNewFolder(...

        private void IgnoreFolders_Load(object sender, EventArgs e)
        {
            this.Text = configInfo.ProductName + " - Exclude disk(s) or folder(s)";
            msgLabel.Text = "";
            //load the current ignore folders array
            listBox1.BeginUpdate(); // Shutdown the painting of the ListBox
            foreach (string str in configInfo.IgnoreFolders)
            { listBox1.Items.Add(str); }
            listBox1.EndUpdate();   // Allow the ListBox to repaint and display the remaining items
            textBox1.Select();
        }

        private void addButton_Click(object sender, EventArgs e)
        {   //Add a new disk or folder
            string newIgnore = textBox1.Text.Trim();
            if ((newIgnore.IndexOf("*") > -1) || (newIgnore.IndexOf("?") > -1))
                msgLabel.Text = "Selection characters (asterisk and question mark) are not allowed";
            else
            {
                AddNewFolder(newIgnore);
                textBox1.Text = 
                msgLabel.Text = "";
            }
            textBox1.Focus();
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {   //Delete selected folders
            if (-1 == listBox1.SelectedIndex)
                return;
            ArrayList remove_folders = new ArrayList();
            listBox1.BeginUpdate(); // Shutdown the painting of the ListBox as items are deleted
            for (int i = 0; i < listBox1.SelectedItems.Count; ++i)
                remove_folders.Add(listBox1.SelectedItems[i].ToString());
            //listBox1.Items.RemoveAt(listBox1.SelectedItems);
            if (remove_folders.Count != 0)
                isDirty = true;
            foreach (string str in remove_folders)
                listBox1.Items.Remove(str);
            listBox1.EndUpdate();   // Allow the ListBox to repaint and display the remaining items
            listBox1.Invalidate();
            textBox1.Focus();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {   //Cancel all changes
            isDirty = false;
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }


        private void exitButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void defaultsButton_Click(object sender, EventArgs e)
        {   //reset ignored folders to defaults
            listBox1.Items.Clear();
            ArrayList ignoreFolders = new ArrayList();
            ignoreFolders = configInfo.DefaultFolders;
            foreach (string str in ignoreFolders)
                listBox1.Items.Add(str);
            isDirty = true;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((char)Keys.Enter == e.KeyChar)
            { //treat as Add button
                string newIgnore = textBox1.Text;
                if ((newIgnore.IndexOf("*") > -1) || (newIgnore.IndexOf("?") > -1))
                    msgLabel.Text = "Wild card selection characters are not allowed";
                else
                {
                    AddNewFolder(textBox1.Text);
                    textBox1.Text = null;
                    msgLabel.Text = " ";
                    e.Handled = true;
                }
                textBox1.Focus();
            }
        }

        private void IgnoreForm_Closing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult.Cancel != this.DialogResult)
            {
                this.DialogResult = DialogResult.OK;
                if (isDirty)
                {
                    ArrayList saveExcludes = new ArrayList();
                    for (int i = 0; i < listBox1.Items.Count; ++i)
                        saveExcludes.Add(listBox1.Items[i].ToString());
                    configInfo.IgnoreFolders = saveExcludes;
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) retired
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions